在iphone中改变方向时重新排列UI按钮



在此处输入图像描述,我有一个视图中的按钮列表,该视图是另一个UIView的子视图。当设备方向更改为横向模式时,我需要按顺序重新排列这些按钮。

他们有办法做到这一点吗?或者我需要通过重新定位所有按钮来手动完成所有这些事情?

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。这允许您将任何可设置动画的视图特性更改包裹在自己的动画中,以便它们在旋转过程中平滑地设置动画,而不是仅捕捉到它们的新值。

但是,实际上,这通常不是解决问题的最佳方法。有关问题的更一般性讨论,请参阅此链接

更改中的CGFrame按钮

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

1您可以使用此

YOUR_OBJECT.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | 
            UIViewAutoresizingFlexibleRightMargin;

2-在中为您的对象使用CGRectMake的框架属性

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

您可以通过进行处理

- (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;
}

最新更新