判断是ipad还是iphone设备。此定义在PayViewControllerDemo-Prefix.pch
定义如下:
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define iPhone UIUserInterfaceIdiomPhone
#define iPad UIUserInterfaceIdiomPad
// ios5下的横屏需要调用的函数
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
ios5可以在每一个视图中取控制视图的方向,以及当视图切换不同方向时,去控制其子视图的布局,具体可以参照下列方式。
//界面视图里面有UIImageView和UIButton空间,当横竖屏时,坐标的改变。
@interfaceViewController ()
{
PayViewController *payview;
UIImageView *drawViewipad;
UIImageView *drawViewihone;
UIButton *consumeBtnipone;
UIButton *consumeBtnipd;
}
//6.0之前
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
if (isPad == iPad)
{
drawViewipad.frame = CGRectMake(306, 150, 412, 120);
consumeBtnipd.frame = CGRectMake(1024/2-280/2, 550, 280, 50);
}
else if (isPad == iPhone)
{
drawViewihone.frame = CGRectMake(155, 50, 170, 60);
consumeBtnipone.frame = CGRectMake(170, 200, 140, 40);
}
}
if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
if (isPad == iPad)
{
drawViewipad.frame = CGRectMake(200, 150, 412, 120);
consumeBtnipd.frame = CGRectMake(768/2-280/2, 550, 280, 50);
}
else if (isPad == iPhone)
{
drawViewihone.frame = CGRectMake(85, 50, 170, 60);
consumeBtnipone.frame = CGRectMake(90, 200, 140, 40);
}
}
returnYES;
}
// ios6下的横屏需要调用的函数
-(BOOL)shouldAutorotate {
return YES;
}
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;//支持4各方向的旋转。
}
然后在plist文件里面找到Supported interface orientations 选项,添加你想支持的方向,都有提示的。
设置这个选项需要和代码一直,否则会出问题。
接着看如何控制每一个界面的方向及在不同方向界面的布局,由于ios6不能单独控制每一个界面的方向,所以需要由主viewcontroller来设置界面的控制,如果你有navgation,那么需要定义一个navgation的类,然后在此类中定义控制子viewcontroller的代码,如下:
//ios6之后需要在top-most controller中来控制方向问题。
-(BOOL)shouldAutorotate
{
return [self.viewControllers.lastObjectshouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [self.viewControllers.lastObjectsupportedInterfaceOrientations];
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.viewControllers.lastObjectpreferredInterfaceOrientationForPresentation];
}
如果没有navgation,那么就需要在appdelegate中去设置这段代码。
接着可以在任何子viewcontroller中来控制不同方向的view布局,可以参考子viewcontroller的代码布局,如下:
//ios6之后需要在top-most controller中来控制方向问题。
- (BOOL)shouldAutorotate
{
returnYES;
}
- (NSUInteger)supportedInterfaceOrientations
{
//判断系统的版本是6.0以上的。
if ([[UIDevicecurrentDevice]systemVersion].floatValue >= 6.0)
{
//判断设备当前的方向,然后重新布局不同方向的操作。
UIInterfaceOrientation currentOrientation = [[UIApplicationsharedApplication] statusBarOrientation];
if (currentOrientation == UIInterfaceOrientationPortrait || currentOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
if (isPad == iPad)
{
drawViewipad.frame = CGRectMake(200, 150, 412, 120);
consumeBtnipd.frame = CGRectMake(768/2-280/2, 550, 280, 50);
}
else if (isPad == iPhone)
{
drawViewihone.frame = CGRectMake(85, 50, 170, 60);
consumeBtnipone.frame = CGRectMake(90, 200, 140, 40);
}
}
if (currentOrientation == UIInterfaceOrientationLandscapeLeft || currentOrientation == UIInterfaceOrientationLandscapeRight)
{
if (isPad == iPad)
{
drawViewipad.frame = CGRectMake(306, 150, 412, 120);
consumeBtnipd.frame = CGRectMake(1024/2-280/2, 550, 280, 50);
}
else if (isPad == iPhone)
{
drawViewihone.frame = CGRectMake(155, 50, 170, 60);
consumeBtnipone.frame = CGRectMake(170, 200, 140, 40);
}
}
}
returnUIInterfaceOrientationMaskAll;
}
这样就能控制不同方向的任何操作和界面布局了。