iOS XIB的屏幕尺寸适配
2017-08-28 本文已影响4757人
Mikebanana
开发中,都会有适应各种屏幕尺寸的需要,因为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 suitParm:CGFloat = (iphone6p ? 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:
image.png就可以打开我们针对屏幕尺寸的约束,比较方便快捷,下面再给一下大家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:
6、7或者6s:
image.png6p、6sp或者7p:
image.png一些小东西,希望对大家有帮助