2013-03-20 77 views
1

我试图在我的应用处于横向模式或纵向模式时在不同位置显示一组图像。我已经搜查,发现参考willAnimateRotationToInterfaceOrientation:duration方法,在这里我补充这样的代码:ios:基于方向的移动图像

- (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,我也遇到同样的问题。我看到了一些涉及多个视图的复杂解决方案,我宁愿避免。有什么建议么?

回答

1

这听起来像你在与autolayout战斗。我能想到的有三种解决方案。

  1. 关闭autolayout。如果您已经自己管理所有视图框架,那么这可能是正确的路径。

  2. 将您的布局代码移动到-viewDidLayoutSubviews。这很快但很快。它也允许autolayout做它的东西主要是

  3. 将适用的旋转约束换掉。这将是最多的工作,但从长远来看,可以说这是国际化等最灵活的工作。

关于三:

约束(NSLayoutConstraint S)是可被检查和IB两者和代码编辑的真实对象。由于布局非常复杂,下面是一个非常简单的例子。假设我在视图控制器的视图中有两个按钮(连接到两个出口buttonOnebuttonTwo),并且我想要以横向方向反转它们的位置。像这样的代码(虽然这种形式不适用)确实有效,并提供了一个如何交换约束的例子。

@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是很多东西。

+0

谢谢。我现在用#2去了。我无法关闭自动布局,因为它对我的其他视图至关重要,我不确定我完全理解#3。你的意思是删除有关图像的限制吗? – 2013-03-20 21:38:18

+0

@CherylYaeger我添加了一些关于选项3的更多信息。 – NJones 2013-03-21 02:13:20

+0

感谢您添加这些额外的信息! – 2013-04-02 18:33:41