当我的应用处于横向与纵向模式时,我正尝试在不同位置显示一组图像。我已经搜索过并找到willAnimateRotationToInterfaceOrientation:duration
方法的引用,我添加了这样的代码:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
self.getStartedImage1.frame = CGRectMake(5, 100, 155, 115);
self.getStartedImage2.frame = CGRectMake(160, 100, 155, 115);
self.getStartedImage3.frame = CGRectMake(315, 100, 155, 115);
}
else
{
self.getStartedImage1.frame = CGRectMake(238, 96, 155, 115);
self.getStartedImage2.frame = CGRectMake(238, 219, 155, 115);
self.getStartedImage3.frame = CGRectMake(238, 342, 155, 115);
}
}
这很有效,直到我意识到如果你在横向模式下进入视图,该方法不会运行。这是有道理的,因为我打开视图而不旋转任何东西。但是,它并没有帮助我解决困境。所以我继续搜索并找到方法viewWillLayoutSubviews
。我为这个方法添加了相同的主体并且它被调用了,但也许为时已晚,因为图像帧的更改没有出现在模拟器中。如果我尝试将代码移动到viewWillAppear
,我会遇到同样的问题。我已经看到一些涉及多个视图的复杂解决方案,我宁愿避免。有什么建议吗?
答案 0 :(得分:1)
听起来你正在与autolayout
作战。我能想到三种解决方案。
关闭autolayout
。如果您已经自己管理了所有视图框架,那么这可能是正确的路径。
将布局代码移至-viewDidLayoutSubviews
。这很hacky但很快。它还允许autolayout
执行主要是的事情。
交换适用的轮换约束。这将是最多的工作,但可以说是国际化等长期最灵活的工作。
关于第三名:
约束(NSLayoutConstraint
s)是可以在IB和代码中检查和编辑的真实对象。由于布局非常复杂,这是一个非常简单的例子。假设我的视图控制器视图中有两个按钮,一个在另一个上面(连接到两个出口buttonOne
和buttonTwo
),我想要以横向方向反转它们的位置。像这样的代码(虽然在这种形式中不可取)确实有效,并提供了如何交换约束的示例。
@implementation MyAutoViewController {
NSArray *_portraitConstraints;
NSArray *_landscapeConstraints;
}
-(void)viewDidLoad{
[super viewDidLoad];
// Clear constraints from Nib.
// PLEASE DO NOT DO THIS PART IN REAL LIFE
NSArray *current = self.view.constraints;
for (NSLayoutConstraint *contsra in current) {
[self.view removeConstraint:contsra];
}
// Set up views dictionary
UIButton *buttonOne = self.buttonOne;
UIButton *buttonTwo = self.buttonTwo;
NSDictionary *viewsDict = NSDictionaryOfVariableBindings(buttonOne, buttonTwo);
// Set distance from left to both buttons to standard space
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[buttonOne]" options:0 metrics:nil views:viewsDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[buttonTwo]" options:0 metrics:nil views:viewsDict]];
// HERE IS WHERE IT GETS INTERESTING
// ### Create portrait constraints ###
_portraitConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[buttonOne]-[buttonTwo]" options:0 metrics:nil views:viewsDict];
// ### Create landscape constraints ###
_landscapeConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[buttonTwo]-[buttonOne]" options:0 metrics:nil views:viewsDict];
// Add the appropriate constraints
[self.view addConstraints:(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))?_portraitConstraints:_landscapeConstraints];
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){
[self.view removeConstraints:_landscapeConstraints];
[self.view addConstraints:_portraitConstraints];
} else {
[self.view removeConstraints:_portraitConstraints];
[self.view addConstraints:_landscapeConstraints];
}
}
我真的建议在WWDC2012上观看autolayout
上的三个视频。 Autolayout
可以解决问题。