iOS XIB的屏幕尺寸适配

释放双眼,带上耳机,听听看~!

开发中,都会有适应各种屏幕尺寸的需要,因为4s年代久远,本文中就没有针对4s的适配(其实屏幕宽度和5s一样,开发中按5s的适配参数来就行了,高度适配的话就把页面调成滑动的就好了),话不多说,先上适配参数,

let iphone5  = UIScreen.instancesRespond(to:#selector(getter: UIScreen.main.currentMode)) ? __CGSizeEqualToSize(CGSize(width:960,height:1336), (UIScreen.main.currentMode?.size)!) : false
let iphone6  = UIScreen.instancesRespond(to:#selector(getter: UIScreen.main.currentMode)) ? __CGSizeEqualToSize(CGSize(width:750,height:1334), (UIScreen.main.currentMode?.size)!) : false
let iphone6p  = UIScreen.instancesRespond(to:#selector(getter: UIScreen.main.currentMode)) ? __CGSizeEqualToSize(CGSize(width:1242,height:2208), (UIScreen.main.currentMode?.size)!) : false
let iphone6pBigMode = UIScreen.instancesRespond(to:#selector(getter: UIScreen.main.currentMode)) ? __CGSizeEqualToSize(CGSize(width:1125,height:2001), (UIScreen.main.currentMode?.size)!) : false
let iphoneX = UIScreen.instancesRespond(to:#selector(getter: UIScreen.main.currentMode)) ? __CGSizeEqualToSize(CGSize(width:1125,height:2436), (UIScreen.main.currentMode?.size)!) : false
let iphoneXR = UIScreen.instancesRespond(to:#selector(getter: UIScreen.main.currentMode)) ? __CGSizeEqualToSize(CGSize(width:828,height:1792), (UIScreen.main.currentMode?.size)!) : false
let iphoneXSM  = UIScreen.instancesRespond(to:#selector(getter: UIScreen.main.currentMode)) ? __CGSizeEqualToSize(CGSize(width:1242,height:2688), (UIScreen.main.currentMode?.size)!) : false
let iphoneXXL = (iphoneX||iphoneXR||iphoneXSM)


//适配参数
let suitParm:CGFloat = ((iphone6p||iphoneXR||iphoneXSM) ? 1.12 : (iphone6 ? 1.0 : (iphone6pBigMode ? 1.01 : (iphoneX ? 1.0 : 0.85))))

接下来只需要在NSLayoutConstraint的extension中添加以下代码,就能在XIB中动态的控制约束的屏幕尺寸适配

import Foundation
import UIKit
extension NSLayoutConstraint {
    @IBInspectable var adapterScreen: Bool {
        get {
            return true
        }
        
        set {
            if newValue {
               self.constant = self.constant * suitParm
            }
          
        }
    }
 
}

因为get是用不到的,所以随便返回了个true
好了,现在我们就可以在XIB上找到我们设置的属性了
选中一个约束后

image.png

然后再到最右边就可以看到这样的一个属性:

我们只需要选中on:

就可以打开我们针对屏幕尺寸的约束,比较方便快捷,下面再给一下大家OC的版本,

/* iOS设备 */
#define kDevice_Is_iPhone4s ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
#define kDevice_Is_iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#define kDevice_Is_iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
#define kDevice_Is_iPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)
#define iPhone6PlusBigMode ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2001), [[UIScreen mainScreen]currentMode].size) : NO)
//适配参数
#define KsuitParam (kDevice_Is_iPhone6Plus ?1.12:(kDevice_Is_iPhone6?1.0:(iPhone6PlusBigMode ?1.01:0.85))) //以6为基准图

新建OC文件 NSLayoutConstraint+IBDesignable
在NSLayoutConstraint+IBDesignable.h里

#import <UIKit/UIKit.h>

@interface NSLayoutConstraint (IBDesignable)

@property(nonatomic, assign) IBInspectable BOOL adapterScreen;

@end

在NSLayoutConstraint+IBDesignable.m文件里

#import "NSLayoutConstraint+IBDesignable.h"

@implementation NSLayoutConstraint (IBDesignable)

- (void)setAdapterScreen:(BOOL)adapterScreen{

if adapterScreen{
    self.constant = self.constant * KsuitParam;
}
}

- (BOOL)adapterScreen{
    return YES;
}

@end

让大家看看效果
5、5s或者SE:

image.png

6、7或者6s:

image.png

6p、6sp或者7p:

image.png

最后 需要注意的是 启动图每个尺寸都要到位 iphoneXR iphoneXM 不到位的话 获取的UIScreen.main.currentMode?.size是不对的

一些小东西,希望对大家有帮助

 

人已赞赏
iOS文章

iOS 给切过圆角的View,加阴影(既切圆角又加阴影)

2019-11-27 20:25:40

iOS文章

iOS 拨打电话三种方式总结

2019-11-29 14:30:31

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索