IOS AutoLayout Change position on Rotation(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 旋转时改变位置
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01