2012-07-30 21 views
2

enter image description herehai,我有一个视图中的按钮列表,这是另一个UIView的子视图。当设备方向改变为横向模式时,我需要按顺序重新排列这些按钮。重新排列UIButtons而iPhone的方向变化?

是他们的任何方式来做到这一点?否则我需要通过重新定位所有按钮来手动完成所有这些操作?

+0

这两种方法参见[这个问题,对于这个问题的一般性讨论(http://stackoverflow.com/questions/11621777/how-to-properly -design-multi-orientation-ipad-application) – Nate 2012-07-30 12:33:55

回答

1

在改变你的按钮的CGFrame的

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    if(interfaceOrientation==UIInterFaceOrientationPortrait) 
    { 
     Button1.frame=...; 
     Button2.frame=....; 
    } 
    else if(interfaceOrientation==UIInterFaceOrientationLandscapeLeft) 
    { 
     Button1.frame=...; 
     Button2.frame=....; 
    } 
    return YES; 
} 
+2

'shouldAutorotateToInterfaceOrientation'用于让你的视图控制器指定**支持哪些**方向。这不是真的用于处理旋转。您也必须在该方法中返回“YES”或“NO”。你根本没有回报价值。 – Nate 2012-07-30 12:39:14

+0

@Nate:是的,你是一半右..我们也可以在这个方法中指定每个方向的坐标。 – 2012-07-30 12:43:22

+1

不,我完全正确。这种方法正是我所说的。 'willRotateToInterfaceOrientation'用于处理变更,一旦决定会有变更。你甚至不会测试旋转是否会被支持,这是**方法的点**(因此是返回值)。 – Nate 2012-07-30 12:53:11

0

1,您可以在

使用与CGRectMake这个

YOUR_OBJECT.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | 
      UIViewAutoresizingFlexibleRightMargin; 

OR

2-使用frame属性为对象

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    if(interfaceOrientation==UIInterFaceOrientationPortrait) 
else if(interfaceOrientation==UIInterFaceOrientationLandscapeLeft) 
3

shouldAutorotateToInterfaceOrientation:,在其他答案中提到,是让您的视图控制器决定是否允许自动旋转到给定的方向。 (你应该返回YESNO)。

如果你真的想编程调整子视图的布局,你会做它在任何

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    [UIView animateWithDuration: duration 
          delay: 0.0f 
         options: UIViewAnimationOptionCurveLinear 
        animations: ^(void) { 

         if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) { 
          self.button.frame = CGRectMake(x1, y1, self.button.frame.size.width, self.button.frame.size.height); 
         } else { 
          self.button.frame = CGRectMake(x2, y2, self.button.frame.size.width, self.button.frame.size.height); 
         } 

        } 
        completion: ^(BOOL finished) { 
         // code to run after animation completes can go here 
        }]; 
} 

,或者您可以使用willAnimateRotationToInterfaceOrientation:duration:一步法旋转。

不仅仅当自动旋转实际上发生时才被调用,但它会给你动画的duration。这使您可以在自己的动画中包装任何动画视图属性更改,以便在旋转过程中平滑地进行动画制作,而不仅仅是捕捉新值。

但是,真的,这是通常不是解决问题的最好方法。见this link for a more general discussion of the issue

0

您可以通过

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

方法处理它。

让你viewController.h类中的两个方法假定他们是 -

-(void)viewForPortraitMode; 
-(void)viewForLandscapeMode; 

而这两者在viewcontroller.m文件中的身体部位。

//Set frame according to you 
-(void)viewForPortraitMode 
{ 
    [btn1 setFrame:CGRectMake(117.0, 383.0, 87.0, 37.0)]; 
    [btn2 setFrame:CGRectMake(40.0, 20.0, 240.0, 324.0)]; 
} 

-(void)viewForLandscapeMode 
{ 
    [btn1 setFrame:CGRectMake(200.0, 252, 87.0, 37.0)]; 
    [btn2 setFrame:CGRectMake(20.0, 7.0, 446.0, 228.0)]; 
} 

并调用这个

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     [self viewForPortraitMode]; 
    } 
    else 
    { 
     [self viewForLandscapeMode]; 
    } 
    return YES; 
} 
+0

但他的观点并未自动更新。它需要关闭视图并重新打开更新 – neerajPK 2012-08-03 04:05:27