我正在尝试使用故事板将UIView集中在横向和纵向方向。下面是一个为此目的而工作的旧弹簧/支柱。以下是我定义的约束。我的视图未显示在横向模式中。 我错过了什么约束来使横向和纵向模式的视图居中?
答案 0 :(得分:1)
为视图(View1)添加约束以水平和垂直居中对齐其superView,并为视图(View1)添加宽度和高度约束,并为添加的宽度和高度约束创建出口。
@interface
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *widthConstraint;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
@end
更新约束:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
self.widthConstraint.constant = ???; //calculated Values
self.heightConstraint.constant = ???? ;
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
self.widthConstraint.constant = ???;
self.heightConstraint.constant = ????;
}
[self.view layoutIfNeeeded];
}
答案 1 :(得分:0)
您可以将属性与约束相关联,并在方向更改时更改其值
@interface
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *leftMarginConstraint;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *topMarginConstraint;
@end
@implementation
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
leftMarginConstraint.constant = ???
topMarginConstraint.constant = ????
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
leftMarginConstraint.constant = ???
topMarginConstraint.constant = ????
}
}