Phone5屏幕没有等比例扩大,直接拉长了,以前的应用在iphone5上直接就是上下两条黑,刚做好ipad开发,最近又要做iphone开发,就在做项目之前把屏幕兼容问题解决了一下。大概可以分为三步:
1、建立xib视图界面时,把view的size都设为Retain4FullScreen
2、把界面分为三个部分,即在superview上添加上中下三个view,顶部和底部的view在不同设备下尺寸不变(即在iphone5和4上都保持相同尺寸),变化的是中间的view,而且变的是高度,这样就为适应屏幕降低了复杂度,并提高了开发效率,还便于设计。(图中三种颜色分别代表上中下三个view,其中中间的view我还加了歌UIScrollView进行测试,不过正常)
3、界面上准备好了,就得搞代码了,代码上首先要判断是否是iphone5,如果是iphone5,就不做处理,如果不是iphone5就对三个view的位置和尺寸进行设置(我这里只改变中间view的尺寸,顶部和底部的view尺寸不变,只是调了view的相对位置,而相对位置则用IOSSDK6.0新出的NSLayoutConstraint里的方法来约束view之间的相对位置),代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
if
(
!iPhone5
)
{
[_topView setTranslatesAutoresizingMaskIntoConstraints : NO ]; [_centerView setTranslatesAutoresizingMaskIntoConstraints : NO ]; [_bottomView setTranslatesAutoresizingMaskIntoConstraints : NO ]; NSDictionary *views = NSDictionaryOfVariableBindings (_topView, _centerView, _bottomView ); [self.view addConstraints : [NSLayoutConstraint constraintsWithVisualFormat : @ "V:|-20-[_topView(100)][_centerView(232)]|" options : 0 metrics : nil views :views ] ]; [self.view addConstraints : [NSLayoutConstraint constraintsWithVisualFormat : @ "V:|-352-[_bottomView(128)]|" options : 0 metrics : nil views :views ] ]; [_testScroll setFrame :CGRectMake ( 0, 0, 320, 232 ) ]; } [_testScroll setContentSize :CGSizeMake ( 320, 500 ) ]; UILabel *label1 = [ [UILabel alloc ]init ]; [label1 setFrame :CGRectMake ( 0, 0, 50, 50 ) ]; label1.text = @ "test"; [label1 setTextColor : [UIColor blackColor ] ]; [_testScroll addSubview :label1 ]; UILabel *label2 = [ [UILabel alloc ]init ]; [label2 setFrame :CGRectMake ( 50, 50, 50, 50 ) ]; label2.text = @ "test2"; [label2 setTextColor : [UIColor blackColor ] ]; [_testScroll addSubview :label2 ]; |
还有判断是否是iphone5的宏:
1 |
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
|
4、最后附上代码