2

自从我升级到 XCODE 4.5 后,我就面临这个问题。

我有各种 UI 元素

UIButton *button1; UIButton *button2; UIButton *button3;

- (void)viewDidLoad
 {
    button1 =[[UIButton alloc]init ];
    button1.backgroundColor=[UIColor yellowColor];
[self.view addSubview:button1];


button2 =[[UIButton alloc]init ];
button2.backgroundColor=[UIColor yellowColor];
[self.view  addSubview:button2];


button3 =[[UIButton alloc]init ];
button3.backgroundColor=[UIColor yellowColor];
[self.view  addSubview:button3];
}

其帧被声明在

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {  
 if(interfaceOrientation==UIInterfaceOrientationPortrait ||[interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown )
 {


 button1.frame=CGRectMake(10,10,10,10);
 button2.frame=CGRectMake(10,30,10,10);

 button3.frame=CGRectMake(10,50,10,10);
 }
      if(interfaceOrientation ==UIInterfaceOrientationLandscapeLeft ||interfaceOrientation==UIInterfaceOrientationLandscapeRight)
{
 button1.frame=CGRectMake(20,10,10,10);
 button2.frame=CGRectMake(20,30,10,10);

 button3.frame=CGRectMake(20,50,10,10);
  }

 return YES;

}

但框架没有在 Xcode 4.5 中设置。在以前的版本中它工作正常。

我需要在我的应用程序中自动调整大小。所以帮助我。

4

2 回答 2

4

您应该需要在 viewController 中实现新方法(在 'ios 6' 中引入)以进行定位

- (BOOL)shouldAutorotate
{

    return TRUE;

}

- (NSUInteger)supportedInterfaceOrientations
{
     return UIInterfaceOrientationMaskAll;


}

并修改您的代码将您的代码放在下面的方法中

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)  interfaceOrientation duration:(NSTimeInterval)duration
{

 }  

还要检查您的窗口,您需要将窗口上的控制器添加为 rootviewController 而不是像下面的 addSubview

self.window.rootViewController=viewController;

于 2012-09-25T12:09:25.143 回答
1

当设备方向改变时,一些类会自动调整大小,例如从纵向到横向,但其他类(如 UILabel 和 UITextView)需要一些配置。

setAutoresizesSubviews属性控制每个对象是否会在其边界更改时自动调整大小。

setAutoresizingMask属性控制每个对象如何自动调整大小UILabel 只需要担心调整其宽度,但由于 UITextView 是可滚动的,它需要在其边界发生变化时调整其宽度和高度。

您还应该确保将shouldAutorotateToInterfaceOrientation方法配置为返回YES;否则当设备方向改变时你的视图不会做任何事情!

示例代码:

[self.myLabel setAutoresizesSubviews:YES];
[self.myLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

[self.myTextView setAutoresizesSubviews:YES];
[self.myTextView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

有关更多详细信息,您可以访问 这里

于 2012-09-25T12:10:19.060 回答