移动应用开发比桌面应用更难开发,同样一个应用的iPad版本不应该是简单地将iPhone界面放大,而是需要重新布局的。

第1代和第2代iPad屏幕的物理尺寸是9.7英寸,分辨率是1024*768像素,第3代iPad采用视网膜屏幕技术,分辨率达到了2048*1536,而iPad mini的分辨率达到了1024*768像素。

在iPhone4之前,屏幕的物理尺寸是3.5英寸,屏幕分辨率是480*320像素。iPhone4 和iPhone 4S采用视网膜屏幕技术,屏幕的物理尺寸为3.5英寸,分辨率达到了960*640像素。iPhone 5采用视网膜屏幕技术,屏幕的物理尺寸为4英寸,分辨率是1136*640

注:屏幕或控件的尺寸以点为单位,在视网膜屏幕技术中,一个点包括了4个像素,而没采用视网膜屏幕技术的一个点包括一个像素

/*1.png  1@2x.png  1@3x.png

  3GS  3.5寸 320*480 @1x

  4/4s 3.5寸  320*480  @2x  

  5/5c/5s  4寸 320*568 @2x

  6  4.7寸 375*667 @3x

  6Plus  5.5寸 414*736 @3x  

  NSLog(@"w:%f h:%f",[[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bound].size.height);

*/

层级的处理:

1.同一个父视图中先加入的view会被盖在下面

2.子视图是跟随父视图进行层级遮掩,如果父视图层级低于其他同级视图,则父视图的子视图也会被盖住,但是子视图和其他视图的子视图是没有关系的

3.交换两个层的视图时需要注意必须填写正确的层数

4.当层交换了之后对应的子视图的数组下标也会进行改变

//交换两个层的视图

[view1 exchangeSubviewAtIndex:0 withSubviewAtIndex:1];

 

//插入一个视图到指定层

UIView *view5 = [[UIView alloc]init];

view5.frame=CGRectMake(7,80,200,200);

[view1 insertSubview:view5 atIndex:5];

[view1 insertSubview:view5 aboveSubview:view2];

[view1 insertSubview:view5 belowSubview:view3];

 

//将一个view放入到顶层

[view1 bringSubviewToFront:view3];

//将一个view放入最底层

[view1 sendSubviewToBack:view2];

 

视图的自适应

1.设置父视图(backview)允许子视图自适用:backview.autoresizesSubviews = YES;

2.设置子视图(topView)的适用方式:topView.autoresizingMask 

 typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {

    UIViewAutoresizingNone                 = 0,

    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,

    UIViewAutoresizingFlexibleWidth        = 1 << 1,

    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,

    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,

    UIViewAutoresizingFlexibleHeight       = 1 << 4,

    UIViewAutoresizingFlexibleBottomMargin = 1 << 5

};

在iPhone竖屏幕中,状态栏占用20点,导航栏(或工具栏)占用44点,标签栏占用49点,实际这些尺寸在iPhone横屏幕和iPad上也保持不变。

绝对布局和相对布局:

  绝对布局就是视图或者控件在屏幕中的位置绝对,它的大小也是绝对的。即使它所在的父容器视图大小变化或者屏幕旋转了,它的位置也不变。相对布局在上述情况下,它的位置是变化的。使用绝对布局还是相对布局取决于应用场景。在IOS6之后,推出Autolayout布局技术。选择xib或故事板文件,打开文件检查器,可取消选中Use Autolayout复选框,然后选中改变布局的控件,打开其尺寸检查器,在View的Autosizing属性中,虚线线段代表是相对距离,实线线段代表是绝对距离,点击可相互切换。

旋转屏幕

 创建工程后,多创建一个

LandscapeViewController : UIViewController并一起创建xib文件,再在ViewController.h中添加两属性

@property(nonatomic,strong) UIView *mainPortraitView;

@property(nonatomic,strong) UIView *mainLandscapeView;
在ViewController.m中添加代码

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    LandscapeViewController *landscapeViewController = [[LandscapeViewController alloc]initWithNibName:@"LandscapeViewController" bundle:NULL];

    (self.mainLandscapeView) = landscapeViewController.view;

    self.mainPortraitView = self.view;

}

-(BOOL)shouldAutorotate{

    return YES;

}

-(NSUInteger)supportedInterfaceOrientations

{

    return UIInterfaceOrientationMaskAllButUpsideDown;

}

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

{

    

 if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {

        NSLog(@"UIInterfaceOrientationLandscapeRight");

        self.view = self.mainLandscapeView;

        self.view.transform = CGAffineTransformMakeRotation(0);

 

        

    }else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){

        NSLog(@"UIInterfaceOrientationLandscapeLeft");

        self.view = self.mainLandscapeView;

//        self.view.transform = CGAffineTransformMakeRotation(-M_PI);

 

        

 

    }else if(toInterfaceOrientation == UIInterfaceOrientationPortrait){

        NSLog(@"UIInterfaceOrientationPortrait");

        self.view = self.mainPortraitView;

        self.view.transform = CGAffineTransformMakeRotation(0);

 

        

 

    }else{

        NSLog(@"UIDeviceOrientationPortraitUpsideDown");

        self.view = self.mainPortraitView;

        self.view.transform = CGAffineTransformMakeRotation(M_PI);

 

 

    }

 

    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

}

 将

LandscapeViewController的View设置成横屏:

 

 

手动旋转(禁止自动旋转,有事件触发旋转)

 [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];

CGRect rect = [UIScreen mainScreen].bounds;

 self.view.bounds = CGRectMake(0.0, 0.0, rect.size.width, rect.size.height); 

self.view.transform = CGAffineTransformMakeRotation(M_PI/2);