iOS7 Storyboard如何指定约束以使横向和纵向视图居中?

时间:2014-06-16 15:48:40

标签: iphone ios7 uiview autolayout uistoryboard

我正在尝试使用故事板将UIView集中在横向和纵向方向。下面是一个为此目的而工作的旧弹簧/支柱。以下是我定义的约束。我的视图未显示在横向模式中。 我错过了什么约束来使横向和纵向模式的视图居中?

Autoresize

enter image description here

2 个答案:

答案 0 :(得分:1)

为视图(View1)添加约束以水平和垂直居中对齐其superView,并为视图(View1)添加宽度和高度约束,并为添加的宽度和高度约束创建出口。

enter image description here

@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 = ????  
}

}