问题描述
是否有可能改变它的限制时,设备旋转?会如何实现这一目标?
Is it possible to change the constraints when the device is rotated? How might this be achieved?
一个简单的例子可以是其中在纵向,一个堆叠在另一个之上的两个图像,但在横向是并排侧
A simple example might be two images which, in portrait, are stacked one above the other, but in landscape are side-by-side.
如果这是不可能的,怎么回事我会完成这个布局?
If this is not possible, how else might I accomplish this layout?
我建我的code视图和约束,而不是使用Interface Builder。
I am constructing my views and constraints in code and not using interface builder.
推荐答案
编辑:是新概念<一href=\"https://developer.apple.com/library/ios/recipes/x$c$c_help-IB_adaptive_sizes/chapters/AboutAdaptiveSizeDesign.html\">Size班的在X code 6推出,可以为特定大小的类在Interface Builder轻松地设置不同的约束。大多数设备(例如,目前所有的iPhone)有一个的紧凑的在横向模式下垂直尺寸类。
Using the new concept of Size Classes introduced in Xcode 6, you can easily setup different constraints for specific size classes in Interface Builder. Most devices (e.g. all current iPhones) have a Compact vertical size class in landscape mode.
这是一般的布局决策比确定设备的方向一个更好的概念。
This is a much better concept for general layout decisions than determining the device's orientation.
话虽这么说,如果你真的需要知道方向, UIDevice.currentDevice()。方向
是要走的路。
That being said, if you really need to know the orientation, UIDevice.currentDevice().orientation
is the way to go.
原帖:
覆盖的UIViewController
的 updateViewConstraints
方法针对具体情况提供布局约束。通过这种方式,布局始终建立正确的方法根据情况。确保他们形成了一套完整的与故事板中创建的约束。您可以使用IB来设置一般的约束和标记那些随时更改,在运行时被删除。
Override the updateViewConstraints
method of UIViewController
to provide layout constraints for specific situations. This way, the layout is always set up the correct way according to situation. Make sure they form a complete set of constraints with those created within the storyboard. You can use IB to set up your general constraints and mark those subject to change to be removed at runtime.
我用下面执行present一组不同的约束每个方向:
I use the following implementation to present a different set of constraints for each orientation:
-(void)updateViewConstraints {
[super updateViewConstraints];
// constraints for portrait orientation
// use a property to change a constraint's constant and/or create constraints programmatically, e.g.:
if (!self.layoutConstraintsPortrait) {
UIView *image1 = self.image1;
UIView *image2 = self.image2;
self.layoutConstraintsPortrait = [[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[image1]-[image2]-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(image1, image2)] mutableCopy];
[self.layoutConstraintsPortrait addObject:[NSLayoutConstraint constraintWithItem:image1 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem: image1.superview attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
[self.layoutConstraintsPortrait addObject:[NSLayoutConstraint constraintWithItem:image2 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:image2.superview attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
}
// constraints for landscape orientation
// make sure they don't conflict with and complement the existing constraints
if (!self.layoutConstraintsLandscape) {
UIView *image1 = self.image1;
UIView *image2 = self.image2;
self.layoutConstraintsLandscape = [[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[image1]-[image2]-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(image1, image2)] mutableCopy];
[self.layoutConstraintsLandscape addObject:[NSLayoutConstraint constraintWithItem:image1 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:image1.superview attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
[self.layoutConstraintsLandscape addObject:[NSLayoutConstraint constraintWithItem:image2 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem: image2.superview attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
}
BOOL isPortrait = UIInterfaceOrientationIsPortrait(self.interfaceOrientation);
[self.view removeConstraints:isPortrait ? self.layoutConstraintsLandscape : self.layoutConstraintsPortrait];
[self.view addConstraints:isPortrait ? self.layoutConstraintsPortrait : self.layoutConstraintsLandscape];
}
现在,你需要做的是触发约束更新每当形势的变化。覆盖 willAnimateRotationToInterfaceOrientation:时间:
来设置动画方向变化约束更新:
Now, all you need to do is trigger a constraint update whenever the situation changes. Override willAnimateRotationToInterfaceOrientation:duration:
to animate the constraint update on orientation change:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
[self.view setNeedsUpdateConstraints];
}
这篇关于我可以使用自动布局为横向和纵向提供不同的约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!