IOS AutoLayout 旋转时改变位置

IOS AutoLayout Change position on Rotation(IOS AutoLayout 旋转时改变位置)

本文介绍了IOS AutoLayout 旋转时改变位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在纵向和横向上并排发送一个容器在另一个容器下方.是否可以仅使用自动布局?我知道我可以以编程方式做到这一点,只是想知道是否可以从布局中做到这一点.

I wanted to sent one container below the other on portrait and side by side on landscape. Is it possible with just auto layout? I know i can do it programmatically, just wondering if it is possible to do it from the layout.

像这样:

推荐答案

好吧,didRotateFromInterfaceOrientation 不是在 iOS 8 上做这些事情的首选方式.我建议你看看traitCollection 和与之关联的视图控制器方法.这里的技巧是在 UIViewController 方法 viewWillTransitionToSize:withTransitionCoordinator: 触发时安装水平约束或垂直约束.

Well didRotateFromInterfaceOrientation is not the preferred way to do these kind of things with iOS 8. I would suggest you to take a look at traitCollection and view controller methods associated with it. The trick here is to install horizontal constraint or vertical constraint when UIViewController method 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

这是两个方向的外观,

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

For iOS7, you basically do the same thing. Instead of overriding viewWillTransitionToSize: you should rather override willAnimateRotationToInterfaceOrientation:duration:. Then you add or remove the constraint inside the method,

@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

这篇关于IOS AutoLayout 旋转时改变位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:IOS AutoLayout 旋转时改变位置

基础教程推荐