大家好。我有一个相当简单的问题。我正在开发一款“富有”iPad应用程序,并且我有两个专门为横向和纵向设计的背景图片。我希望这个ImageView能够根据设备的方向自动切换(就像苹果的大多数iPad应用程序一样)。
有人能够指点我吗?我假设这是在viewDidLoad中完成的某些操作。
大家好。我有一个相当简单的问题。我正在开发一款“富有”iPad应用程序,并且我有两个专门为横向和纵向设计的背景图片。我希望这个ImageView能够根据设备的方向自动切换(就像苹果的大多数iPad应用程序一样)。
有人能够指点我吗?我假设这是在viewDidLoad中完成的某些操作。
你可以做的最好的事情是根据你的界面方向更改子视图框架的框架。你可以这样做:
#pragma mark -
#pragma mark InterfaceOrientationMethods
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (UIInterfaceOrientationIsPortrait(interfaceOrientation) || UIInterfaceOrientationIsLandscape(interfaceOrientation));
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){
//self.view = portraitView;
[self changeTheViewToPortrait:YES andDuration:duration];
}
else if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){
//self.view = landscapeView;
[self changeTheViewToPortrait:NO andDuration:duration];
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
- (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
if(portrait){
//change the view and subview frames for the portrait view
}
else{
//change the view and subview frames for the landscape view
}
[UIView commitAnimations];
}
UIInterfaceOrientationIsPortrait()
和UIInterfaceOrientationIsLandscape()
对于这些情况的可读性确实有很大提高...但是,无论如何都是个好的解决方案! - Nate- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration {
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation ==
UIInterfaceOrientationPortraitUpsideDown) {
[brownBackground setImage:[UIImage imageNamed:@"Portrait_Background.png"]];
} else {
[brownBackground setImage:[UIImage imageNamed:@"Landscape_Background.png"]];
}
}
根据方向改变已声明的UIImageView的背景。唯一的缺点是,当前的背景图像在界面构建器中不可见,因为它是通过代码处理的。
对于Madhup的方法,这里有一个小修改,很棒。我发现需要在viewDidLoad中添加以下内容,以设置竖屏或横屏的初始背景图片:
// set background image
if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"portraitBG.png"]];
} else {
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"landscapeBG.png"]];
}
如果您正在编写一个小型的自我感知控件,可以通过监视 bounds.width > bounds.height
来完全封装它在您的 UIView 中。
class MyView: UIView {
override func layoutSubviews() {
super.layoutSubviews()
if bounds.height > bounds.width {
println("PORTRAIT. some bounds-impacting event happened")
} else {
println("LANDSCAPE")
}
}
}