自动布局和iPad的约束 [英] Autolayout and Constraints for iPad

查看:161
本文介绍了自动布局和iPad的约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑关于C 4.5×$ C $的新的自动排版功能。

I'm quite confused about the new auto layout feature of xCode 4.5.

下面是我想做的事情,

通过设置故事板,我有这样的纵向视图设置。

By setting the storyboard, I have this portrait view set up.

通过使用自动布局和约束(和引脚),我怎么能转换的布局时,其翻转到风景这样吗?

By using autolayout and constraints(and pins), how can I transform the layout when its flipped to landscape like this?

我试图编码和改变的CGRect(尺寸和坐标位置)的意见时,园景,但都无济于事。

I tried coding and changing the CGRect(size and coordinate location) of the views when its landscaped but to no avail.

推荐答案

NSLayoutConstraints更换为自动布局CGRects的需要。首先,描述文字布局。以下是我会形容你的肖像例如:

NSLayoutConstraints replace the need for CGRects in Auto Layout. First, describe your layout in words. Here's how I'd describe your portrait example:


  • 红色的宽度是它的父的。
  • 60%
  • 蓝色的身高是它的父的。
  • 55%
  • 蓝的左和放大器;右边缘触摸它的父的。

  • 红色的左边缘触及它的父的,红的右边缘,靠近黄的左边缘,和黄色的右边缘触摸它的父的。

  • 蓝色的顶边抚摸它的父的,蓝色的底部边缘接近红色的顶边,红色的底边接触它的父的。

  • 蓝的底部边缘接近黄色的顶边,和黄色的底部边缘接触它的父的。

  • Red's width is 60% of its superview's.
  • Blue's height is 55% of its superview's.
  • Blue's left & right edges are touching its superview's.
  • Red's left edge is touching its superview's, red's right edge is close to yellow's left edge, and yellow's right edge is touching its superview's.
  • Blue's top edge is touching its superview's, blue's bottom edge is close to red's top edge, and red's bottom edge is touching its superview's.
  • Blue's bottom edge is close to yellow's top edge, and yellow's bottom edge is touching its superview's.

下面是删除上海华现有的限制,然后应用新的约束条件给定的接口方向。方法

Here's a method that removes superview's existing constraints, then applies new constraints for the given interface orientation.

- (void) buildConstriantsForInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Remove existing constraints.
    [superview removeConstraints:superview.constraints] ;
    // Build an array to hold new constraints.
    NSMutableArray* constraints = [NSMutableArray new] ;

    // Add 2 constraints that apply to both Portrait & Landscape orientations.
    [constraints addObject:[NSLayoutConstraint constraintWithItem:red  attribute:NSLayoutAttributeWidth  relatedBy:NSLayoutRelationEqual  toItem:superview  attribute:NSLayoutAttributeWidth  multiplier:0.6  constant:0]] ;
    [constraints addObject:[NSLayoutConstraint constraintWithItem:blue  attribute:NSLayoutAttributeHeight  relatedBy:NSLayoutRelationEqual  toItem:superview  attribute:NSLayoutAttributeHeight  multiplier:0.55  constant:0]] ;

    // Build a dictionary to store references to NSViews.
    NSDictionary* views = NSDictionaryOfVariableBindings(superview, blue, red, yellow) ;
    // To eliminate repeated NSLayoutConstraint code, build an array of Format Strings with which to build constraints.
    NSArray* formatStrings ;
    if ( UIInterfaceOrientationIsPortrait(interfaceOrientation) ) {
        formatStrings = @[@"H:|[blue]|", @"H:|[red]-[yellow]|", @"V:|[blue]-[red]|", @"V:[blue]-[yellow]|"] ;
    }
    else {
        formatStrings = @[@"H:|[blue]-[yellow]|", @"H:|[red]-[yellow]", @"V:|[blue]-[red]|", @"V:|[yellow]|"] ;
    }

    for ( NSString* formatString in formatStrings ) {
        [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:formatString  options:0  metrics:nil  views:views]] ;
    }

    // Add the newly created constraints.
    [superview addConstraints:constraints] ;
}

您可以调用该方法时的观点负载或旋转。

You can call that method whenever the view loads or rotates.

-(void) viewDidLoad {
    superview.translatesAutoresizingMaskIntoConstraints = NO ;
    [self buildConstriantsForInterfaceOrientation:self.interfaceOrientation] ;
}

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [self buildConstriantsForInterfaceOrientation:toInterfaceOrientation] ;
}

这篇关于自动布局和iPad的约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆