IOS AutoLayout在旋转时更改位置

时间:2014-11-21 17:32:59

标签: ios objective-c autolayout nsautolayout

我想把一个容器放在另一个容器下面,然后在横向上并排放置。只有自动布局才有可能吗?我知道我可以通过编程方式完成它,只是想知道是否可以从布局中做到这一点。

喜欢这个:

Desired Behaviour

2 个答案:

答案 0 :(得分:0)

是的,我认为会有一些代码。所以我相信这里涉及的2个约束是水平间距和垂直间距(编辑 - >引脚 - >水平/垂直)。

在纵向布局中排列A和B,选择这两个元素,然后添加这两个约束。在头文件中为这两个约束创建IBOutlets(就像你对标签,按钮等一样)

然后,您可以在" didRotateFromInterfaceOrientation"中更改这些约束的值。委托方法(constraint_variable_name.constant = 0;)。如果其中任何一项不清楚,或者您需要我详细说明它的任何部分,请告诉我。

答案 1 :(得分:0)

那么 didRotateFromInterfaceOrientation 不是用iOS 8做这些事情的首选方法。我建议你看一下traitCollection并查看与之关联的控制器方法。这里的技巧是在 UIViewController 方法 viewWillTransitionToSize:withTransitionCoordinator :trigger时安装水平约束或垂直约束。

这是我做的方式,

@interface ViewController ()

@property (nonatomic, weak) UIView *aContainerView;
@property (nonatomic, weak) UIView *bContainerView;

@property (nonatomic, strong) NSArray *horizontalOrientationConstraints;
@property (nonatomic, strong) NSArray *verticalOrientationConstraints;

@end

@implementation ViewController

#pragma mark - Getters

- (NSArray *)horizontalOrientationConstraints
{
    if (!_horizontalOrientationConstraints) {
        NSLayoutConstraint *equalWidthConstraints = [NSLayoutConstraint constraintWithItem:self.aContainerView
                                                                                 attribute:NSLayoutAttributeWidth
                                                                                 relatedBy:NSLayoutRelationEqual
                                                                                    toItem:self.bContainerView
                                                                                 attribute:NSLayoutAttributeWidth
                                                                                multiplier:1.0
                                                                                  constant:0];

        NSArray *vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[aContainerView][bContainerView]|"
                                                                        options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom
                                                                        metrics:nil views:@{@"aContainerView": self.aContainerView, @"bContainerView": self.bContainerView}];
        NSArray *hConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[aContainerView]|"
                                                                        options:0
                                                                        metrics:nil
                                                                          views:@{@"aContainerView": self.aContainerView}];
        NSArray *constraints = [vConstraints arrayByAddingObjectsFromArray:hConstraints];
        _horizontalOrientationConstraints = [constraints arrayByAddingObject:equalWidthConstraints];

    }
    return _horizontalOrientationConstraints;
}


- (NSArray *)verticalOrientationConstraints
{
    if (!_verticalOrientationConstraints) {
        NSLayoutConstraint *equalHeightConstraints = [NSLayoutConstraint constraintWithItem:self.aContainerView
                                                                                  attribute:NSLayoutAttributeHeight
                                                                                  relatedBy:NSLayoutRelationEqual
                                                                                     toItem:self.bContainerView
                                                                                  attribute:NSLayoutAttributeHeight
                                                                                 multiplier:1.0
                                                                                   constant:0];


        NSArray *vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[aContainerView][bContainerView]|"
                                                                        options:NSLayoutFormatAlignAllLeft | NSLayoutFormatAlignAllRight
                                                                        metrics:nil views:@{@"aContainerView": self.aContainerView, @"bContainerView": self.bContainerView}];
        NSArray *hConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[aContainerView]|"
                                                                        options:0
                                                                        metrics:nil
                                                                          views:@{@"aContainerView": self.aContainerView}];
        NSArray *constraints = [vConstraints arrayByAddingObjectsFromArray:hConstraints];
        _verticalOrientationConstraints = [constraints arrayByAddingObject:equalHeightConstraints];

    }
    return _verticalOrientationConstraints;
}

#pragma mark - 


- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *aContainerView = [self viewWithLabelText:@"A" andBackgroundColor:[UIColor yellowColor]];
    UIView *bContainerView = [self viewWithLabelText:@"B" andBackgroundColor:[UIColor greenColor]];

    [self.view addSubview:aContainerView];
    [self.view addSubview:bContainerView];

    self.aContainerView = aContainerView;
    self.bContainerView = bContainerView;

    CGSize viewSize = self.view.bounds.size;

    if (viewSize.width > viewSize.height) {
        [NSLayoutConstraint activateConstraints:self.horizontalOrientationConstraints];
    } else {
        [NSLayoutConstraint activateConstraints:self.verticalOrientationConstraints];
    }
}

- (UIView *)viewWithLabelText:(NSString *)text andBackgroundColor:(UIColor *)color
{
    UIView *aContainerView = [[UIView alloc] init];
    aContainerView.backgroundColor = [UIColor blackColor];
    aContainerView.translatesAutoresizingMaskIntoConstraints = NO;

    UIView *aView = [[UIView alloc] init];
    aView.translatesAutoresizingMaskIntoConstraints = NO;
    aView.backgroundColor = color;

    UILabel *aLabel = [[UILabel alloc] init];
    aLabel.translatesAutoresizingMaskIntoConstraints = NO;
    aLabel.text = text;
    aLabel.font = [UIFont systemFontOfSize:80];

    [aView addSubview:aLabel];

    [aContainerView addSubview:aView];


    NSLayoutConstraint *centerXConstraints = [NSLayoutConstraint constraintWithItem:aView
                                                                          attribute:NSLayoutAttributeCenterX
                                                                          relatedBy:NSLayoutRelationEqual
                                                                             toItem:aLabel
                                                                          attribute:NSLayoutAttributeCenterX
                                                                         multiplier:1.0
                                                                           constant:0];
    NSLayoutConstraint *centerYConstraints = [NSLayoutConstraint constraintWithItem:aView
                                                                          attribute:NSLayoutAttributeCenterY
                                                                          relatedBy:NSLayoutRelationEqual
                                                                             toItem:aLabel
                                                                          attribute:NSLayoutAttributeCenterY
                                                                         multiplier:1.0
                                                                           constant:0];
    [aContainerView addConstraints:@[centerXConstraints, centerYConstraints]];


    NSString *hConstraintsFormat = @"V:|-10-[view]-10-|";
    NSString *vConstraintsFormat = @"H:|-10-[view]-10-|";

    [aContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:hConstraintsFormat
                                                                                options:0
                                                                                metrics:nil
                                                                                  views:@{@"view": aView}]];
    [aContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vConstraintsFormat
                                                                                options:0
                                                                                metrics:nil
                                                                                  views:@{@"view": aView}]];

    return aContainerView;
}



 - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    NSArray *constraintsToDeactivate;
    NSArray *constraintsToActivate;

    if (size.width > size.height) {
        constraintsToActivate = self.horizontalOrientationConstraints;
        constraintsToDeactivate = self.verticalOrientationConstraints;
    } else {
        constraintsToActivate = self.verticalOrientationConstraints;
        constraintsToDeactivate = self.horizontalOrientationConstraints;
    }

    [NSLayoutConstraint deactivateConstraints:constraintsToDeactivate];
    [NSLayoutConstraint activateConstraints:constraintsToActivate];
    [self.view layoutIfNeeded];
}

@end

以下是它们如何查找两个方向,

enter image description here

enter image description here

对于iOS7,你基本上做同样的事情。而不是重写viewWillTransitionToSize:你应该覆盖willAnimateRotationToInterfaceOrientation:duration:。然后在方法中添加或删除约束

@interface ViewController ()

@property (nonatomic, weak) UIView *aContainerView;
@property (nonatomic, weak) UIView *bContainerView;

@property (nonatomic, assign) BOOL touchExited;
@property (nonatomic, assign) NSUInteger count;
@property (nonatomic, weak) NSTimer *countChangeTimer;
@property (nonatomic, weak) UIButton *theCountButton;

@property (nonatomic, strong) NSArray *horizontalOrientationConstraints;
@property (nonatomic, strong) NSArray *verticalOrientationConstraints;

@end

@implementation ViewController

#pragma mark - Getters

- (NSArray *)horizontalOrientationConstraints
{
    if (!_horizontalOrientationConstraints) {
        NSLayoutConstraint *equalWidthConstraints = [NSLayoutConstraint constraintWithItem:self.aContainerView
                                                                                 attribute:NSLayoutAttributeWidth
                                                                                 relatedBy:NSLayoutRelationEqual
                                                                                    toItem:self.bContainerView
                                                                                 attribute:NSLayoutAttributeWidth
                                                                                multiplier:1.0
                                                                                  constant:0];

        NSArray *vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[aContainerView][bContainerView]|"
                                                                        options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom
                                                                        metrics:nil views:@{@"aContainerView": self.aContainerView, @"bContainerView": self.bContainerView}];
        NSArray *hConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[aContainerView]|"
                                                                        options:0
                                                                        metrics:nil
                                                                          views:@{@"aContainerView": self.aContainerView}];
        NSArray *constraints = [vConstraints arrayByAddingObjectsFromArray:hConstraints];
        _horizontalOrientationConstraints = [constraints arrayByAddingObject:equalWidthConstraints];

    }
    return _horizontalOrientationConstraints;
}


- (NSArray *)verticalOrientationConstraints
{
    if (!_verticalOrientationConstraints) {
        NSLayoutConstraint *equalHeightConstraints = [NSLayoutConstraint constraintWithItem:self.aContainerView
                                                                                  attribute:NSLayoutAttributeHeight
                                                                                  relatedBy:NSLayoutRelationEqual
                                                                                     toItem:self.bContainerView
                                                                                  attribute:NSLayoutAttributeHeight
                                                                                 multiplier:1.0
                                                                                   constant:0];


        NSArray *vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[aContainerView][bContainerView]|"
                                                                        options:NSLayoutFormatAlignAllLeft | NSLayoutFormatAlignAllRight
                                                                        metrics:nil views:@{@"aContainerView": self.aContainerView, @"bContainerView": self.bContainerView}];
        NSArray *hConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[aContainerView]|"
                                                                        options:0
                                                                        metrics:nil
                                                                          views:@{@"aContainerView": self.aContainerView}];
        NSArray *constraints = [vConstraints arrayByAddingObjectsFromArray:hConstraints];
        _verticalOrientationConstraints = [constraints arrayByAddingObject:equalHeightConstraints];

    }
    return _verticalOrientationConstraints;
}

#pragma mark -

- (void)invalidateTimer
{
    if (self.countChangeTimer) {
        [self.countChangeTimer invalidate];
    }
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *aContainerView = [self viewWithLabelText:@"A" andBackgroundColor:[UIColor yellowColor]];
    UIView *bContainerView = [self viewWithLabelText:@"B" andBackgroundColor:[UIColor greenColor]];

    [self.view addSubview:aContainerView];
    [self.view addSubview:bContainerView];
    self.aContainerView = aContainerView;
    self.bContainerView = bContainerView;

    CGSize viewSize = self.view.bounds.size;

    if (viewSize.width > viewSize.height) {
        [self.view addConstraints:self.horizontalOrientationConstraints];
    } else {
        [self.view addConstraints:self.verticalOrientationConstraints];
    }
}

- (UIView *)viewWithLabelText:(NSString *)text andBackgroundColor:(UIColor *)color
{
    UIView *aContainerView = [[UIView alloc] init];
    aContainerView.backgroundColor = [UIColor blackColor];
    aContainerView.translatesAutoresizingMaskIntoConstraints = NO;

    UIView *aView = [[UIView alloc] init];
    aView.translatesAutoresizingMaskIntoConstraints = NO;
    aView.backgroundColor = color;

    UILabel *aLabel = [[UILabel alloc] init];
    aLabel.translatesAutoresizingMaskIntoConstraints = NO;
    aLabel.text = text;
    aLabel.font = [UIFont systemFontOfSize:80];

    [aView addSubview:aLabel];

    [aContainerView addSubview:aView];


    NSLayoutConstraint *centerXConstraints = [NSLayoutConstraint constraintWithItem:aView
                                                                          attribute:NSLayoutAttributeCenterX
                                                                          relatedBy:NSLayoutRelationEqual
                                                                             toItem:aLabel
                                                                          attribute:NSLayoutAttributeCenterX
                                                                         multiplier:1.0
                                                                           constant:0];
    NSLayoutConstraint *centerYConstraints = [NSLayoutConstraint constraintWithItem:aView
                                                                          attribute:NSLayoutAttributeCenterY
                                                                          relatedBy:NSLayoutRelationEqual
                                                                             toItem:aLabel
                                                                          attribute:NSLayoutAttributeCenterY
                                                                         multiplier:1.0
                                                                           constant:0];
    [aContainerView addConstraints:@[centerXConstraints, centerYConstraints]];


    NSString *hConstraintsFormat = @"V:|-10-[view]-10-|";
    NSString *vConstraintsFormat = @"H:|-10-[view]-10-|";

    [aContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:hConstraintsFormat
                                                                                options:0
                                                                                metrics:nil
                                                                                  views:@{@"view": aView}]];
    [aContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vConstraintsFormat
                                                                                options:0
                                                                                metrics:nil
                                                                                  views:@{@"view": aView}]];

    return aContainerView;
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration{
    NSArray *constraintsToDeactivate;
    NSArray *constraintsToActivate;

        if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
            constraintsToActivate = self.horizontalOrientationConstraints;
            constraintsToDeactivate = self.verticalOrientationConstraints;
        } else {
            constraintsToActivate = self.verticalOrientationConstraints;
            constraintsToDeactivate = self.horizontalOrientationConstraints;
        }
        [self.view removeConstraints:constraintsToDeactivate];
        [self.view addConstraints:constraintsToActivate];

}

@end