简体   繁体   English

如何使用自动布局为纵向和横向方向配置不同的布局?

[英]How to configure different layouts for Portrait and Landscape Orientations using Auto Layout?

I have a view like in attached image 我有一个像附加图像的视图

在此输入图像描述

Now I am adding constraints such that the UITextview in the view has to be on the right hand side of the screen when orientation is changed to landscape. 现在我添加约束,以便当方向更改为横向时,视图中的UITextview必须位于屏幕的右侧。 On UItextview, I have added below constraints, 在UItextview上,我添加了以下约束,

  1. Trailing Space to : Superview 尾随空间:Superview
  2. Bottom Space to : Superview 底部空间:Superview

These constraints though displayed some warnings on ambugity, did the job for me. 这些限制虽然显示了一些关于ambugity的警告,但是我的工作也是如此。 Below is the screenshot of landscape mode. 以下是横向模式的屏幕截图。

在此输入图像描述

My problem is though the UItextview is moved to right side, I want some additional width from top of superview when it is in landscape mode. 我的问题是虽然UItextview被移动到右侧,但是当它处于横向模式时,我想从superview顶部获得一些额外的宽度。 In other words, I want the UITextview to be moved a little downward from where it is now in landscape mode. 换句话说,我希望UITextview从现在处于横向模式的位置稍微向下移动。 I am pondering how to do that using auto layout in IB and I am not able to figure that how. 我正在考虑如何在IB中使用自动布局来做到这一点,我无法弄清楚如何做到这一点。

Any suggestions please. 请给我任何建议。

You can do this with constraints in several ways, but there's no way to do this automatically with just constraints you make in IB. 您可以通过多种方式使用约束来完成此操作,但只有在IB中进行限制时,无法自动执行此操作。 By using both the multiplier and constant values in the method, constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:, you can have one constraint that evaluates to different distances in portrait and landscape. 通过在方法中使用乘数和常量值,constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:,您可以使用一个约束来计算纵向和横向中的不同距离。 It's a pain to do the calculations to figure out what to use for those values, so I've written a category on NSLayoutConstraint to do that. 进行计算以找出用于这些值的内容是很痛苦的,所以我在NSLayoutConstraint上编写了一个类别来做到这一点。 An example of one of those methods, is this: 其中一种方法的一个例子是:

+(NSLayoutConstraint *)topConstraintForView:(UIView *)subview viewAttribute:(NSLayoutAttribute) att superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
    CGFloat multiplier = (pValue - lValue)/(superview.bounds.size.height - superview.bounds.size.width);
    CGFloat constant = pValue - (superview.bounds.size.height * multiplier);
    NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:att relatedBy:0 toItem:superview attribute:NSLayoutAttributeBottom multiplier:multiplier constant:constant];
     NSLog(@"top coeffs: %f   %f",multiplier,constant);
    return con;
}

The way you use these, is to add the starting portrait constraint in the storyboard, but check the box, "Placeholder - Remove at build time" in the attributes inspector for the constraint, and then replace it in viewDidLoad, like this: 您使用这些方法的方法是在故事板中添加起始纵向约束,但在属性检查器中选中“占位符 - 在构建时删除”框中的约束,然后在viewDidLoad中替换它,如下所示:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addConstraint:[NSLayoutConstraint topConstraintForView:self.textView viewAttribute:NSLayoutAttributeTop superview:self.view portraitValue:225 landscapeValue:50]];
}

This will automatically adjust the position of the text view based on the rotation of the device without any further code. 这将根据设备的旋转自动调整文本视图的位置,而无需任何其他代码。 You might have to change the width of the text fields to get everything to fit properly -- I enclosed them and the "Get" labels in a view to more easily position them as a group. 您可能需要更改文本字段的宽度以使所有内容都适合 - 我将它们和视图中的“获取”标签括起来,以便更轻松地将它们定位为一个组。 That view and the text view had height and width constraints, as well as top and left for the view, and top and right for the text view. 该视图和文本视图具有高度和宽度约束,以及视图的顶部和左侧,以及文本视图的顶部和右侧。 The category has methods to adjust the other constraints as well, and can be found at http://jmp.sh/b/S4exMJBWftlCeO5GybNO . 该类别还有调整其他约束的方法,可以在http://jmp.sh/b/S4exMJBWftlCeO5GybNO找到。

The other way to do this, is to make IBOutlets to the constraints you make in IB, and adjust them (the constant value), or delete some and remake other ones, in one of the rotation callback methods. 另一种方法是将IBOutlets用于你在IB中制作的约束,并在其中一个旋转回调方法中调整它们(常量值),或者删除一些并重新制作其他的约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS自动布局:如何在Xcode故事板中为iPad版和iPad肖像设计不同的布局? - iOS auto layout: how to design different layouts for iPad landscape and iPad portrait in Xcode storyboard? 如何在不同的iPhone屏幕尺寸下使用纵向和横向自动布局 - how to use auto layout in portrait and landscape for different iphone screen sizes 不同的横向和纵向用户界面 - Different user interfaces for landscape and portrait orientations 纵向和横向模式下的不同布局 - Different layouts in portrait and landscape mode 如何在flutter中显示横向和纵向的不同布局 - How to show different layout for landscape and portrait in flutter 自动布局:纵向到横向不一致 - Auto Layout: portrait to landscape inconsistencies iPad横向和纵向尺寸等级的不同布局 - iPad Landscape and Portrait different layouts with Size Class 自动布局-使用约束优先级使用一个Storyboard的纵向和横向? - Auto-Layout - Portrait and Landscape with one Storyboard using constraint priorities? 我可以使用自动布局为横向和纵向提供不同的约束吗? - Can I use autolayout to provide different constraints for landscape and portrait orientations? UIImageView(横向,纵向)对齐问题的自动布局 - Auto layout on UIImageView (landscape, portrait) alignment issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM