【发布时间】:2014-08-06 12:11:21
【问题描述】:
我正在尝试使用故事板将 UIView 居中用于横向和纵向。下面是为此目的工作的旧弹簧/支柱。下面还有我定义的约束。我的视图在横向模式下未居中显示。 横向和纵向模式的视图居中缺少什么约束?
【问题讨论】:
标签: iphone ios7 uiview autolayout uistoryboard
我正在尝试使用故事板将 UIView 居中用于横向和纵向。下面是为此目的工作的旧弹簧/支柱。下面还有我定义的约束。我的视图在横向模式下未居中显示。 横向和纵向模式的视图居中缺少什么约束?
【问题讨论】:
标签: iphone ios7 uiview autolayout uistoryboard
为视图(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];
}
【讨论】:
您可以将属性与约束相关联,并在方向更改时更改其值
@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 = ????
}
}
【讨论】: