• <small id='dpegE'></small><noframes id='dpegE'>

    1. <tfoot id='dpegE'></tfoot>
        <bdo id='dpegE'></bdo><ul id='dpegE'></ul>

    2. <legend id='dpegE'><style id='dpegE'><dir id='dpegE'><q id='dpegE'></q></dir></style></legend>
      1. <i id='dpegE'><tr id='dpegE'><dt id='dpegE'><q id='dpegE'><span id='dpegE'><b id='dpegE'><form id='dpegE'><ins id='dpegE'></ins><ul id='dpegE'></ul><sub id='dpegE'></sub></form><legend id='dpegE'></legend><bdo id='dpegE'><pre id='dpegE'><center id='dpegE'></center></pre></bdo></b><th id='dpegE'></th></span></q></dt></tr></i><div id='dpegE'><tfoot id='dpegE'></tfoot><dl id='dpegE'><fieldset id='dpegE'></fieldset></dl></div>

        IOS AutoLayout 旋转时改变位置

        时间:2023-09-09
            <i id='esgvv'><tr id='esgvv'><dt id='esgvv'><q id='esgvv'><span id='esgvv'><b id='esgvv'><form id='esgvv'><ins id='esgvv'></ins><ul id='esgvv'></ul><sub id='esgvv'></sub></form><legend id='esgvv'></legend><bdo id='esgvv'><pre id='esgvv'><center id='esgvv'></center></pre></bdo></b><th id='esgvv'></th></span></q></dt></tr></i><div id='esgvv'><tfoot id='esgvv'></tfoot><dl id='esgvv'><fieldset id='esgvv'></fieldset></dl></div>

              <small id='esgvv'></small><noframes id='esgvv'>

            • <tfoot id='esgvv'></tfoot>

                <legend id='esgvv'><style id='esgvv'><dir id='esgvv'><q id='esgvv'></q></dir></style></legend>

                  <tbody id='esgvv'></tbody>
                • <bdo id='esgvv'></bdo><ul id='esgvv'></ul>
                  本文介绍了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 8 图像缩放和自动布局 下一篇:自动布局、屏幕旋转和 UIView 动画

                  相关文章

                  <tfoot id='dy170'></tfoot>

                      <bdo id='dy170'></bdo><ul id='dy170'></ul>

                    <small id='dy170'></small><noframes id='dy170'>

                  1. <i id='dy170'><tr id='dy170'><dt id='dy170'><q id='dy170'><span id='dy170'><b id='dy170'><form id='dy170'><ins id='dy170'></ins><ul id='dy170'></ul><sub id='dy170'></sub></form><legend id='dy170'></legend><bdo id='dy170'><pre id='dy170'><center id='dy170'></center></pre></bdo></b><th id='dy170'></th></span></q></dt></tr></i><div id='dy170'><tfoot id='dy170'></tfoot><dl id='dy170'><fieldset id='dy170'></fieldset></dl></div>
                  2. <legend id='dy170'><style id='dy170'><dir id='dy170'><q id='dy170'></q></dir></style></legend>