[英]Change nib programmatically when orientation changes
我在初始化中像这样加载笔尖:
self = [super initWithNibName:@"PageView_iPhone" bundle:nil];
但是,如果方向发生变化,如何在运行时(即init之后)进行更改? initWithNibName
将不起作用。
如果您确实想加载其他笔尖,则可以使用:
[[NSBundle mainBundle] loadNibNamed:@"PageView_iPhone" owner:self options:nil];
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"PageView_iPhone"
owner:self
options:nil];
PageView_iPhone* myView = [nibViews objectAtIndex:0];
在初始化过程中,我设置了我的视图(以编程方式创建它们),并在视图控制器中为每个视图创建了额外的CGPoint用于中心位置或CGRect用于框架。
在init中创建视图后,我调用名为layoutViewsForOrientation的方法来设置初始位置,并传递当前状态栏的方向。 当方向改变时,我使用以下代码:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
BOOL result = NO;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
result = (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
result = YES;
}
if( result == YES ) [self layoutViewsForOrientation:interfaceOrientation];
return result;
}
可能有一种更简单的方法,但这很适合我的需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.