前些日子出来了iphone6和6plus 使的iOS的适配再也不适合用绝对布局了 本人之前一直是做android开发的 最近开始学起了ios 于是也自然而然的学起了蛋疼的autolayout适配 现在在storyboard中是能够熟练用约束适配了 但是总有些特殊情况需要在逻辑代码中适配 于是我想起了android的屏幕密度概念 就想写个工具类 使得在ios的controller中也能轻松的设置控件的具体大小和比例 并自动适配
-
必须要在项目启用了autolayout之后才能用 因为工具类里是用vfl来设置大小的
-
android的屏幕密度其实不是这个意思 计算也比这个复杂的多 不过是借个概念 实现个差不多的效果
-
这个工具类主要是用来在使用autolayout布局时设置某个控件一个具体的像素大小的 只不过这个控件的这个具体大小会根据屏幕自动拉伸适配
-
设置具体大小的原始标准是4寸屏,设大小时要参照这个 也就是说 比如界面上有个button 在4寸屏时希望他宽高都是50像素 那就直接把他设为50就行了 到了4.7寸和5.5寸时 他的大小自然会等比扩大,至于这个button扩大之后的位置 这个当然在用autolayout的布局约束时就已经控制好的了
-
这个适配方法可能不是很好,可能是我才初学 不清楚autolayout的强大 因为不是所有的界面需要显示的控件都能通过他和周围控件的约束就能适配的 直接约束控件大小的话在4寸屏显示正好 但是到了5.5寸虽然位置什么的都正常 但是大小却会变的偏小了 但我觉得autolayout不应该解决不了这个问题的啊 所以在此做个保留 可能是我还没真正领悟到autolayout 下面贴上工具类代码 仅供参考吧
-
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface Density : NSObject /** 模拟android屏幕密度的概念 */ +(NSInteger)pixelsToDip:(float) pixels; // pixels转dip +(void)setWidth:(UIView *)view currentView:(UIView *)currentView pixels:(float)pixels; // 设置控件自动适配的width +(void)setHeight:(UIView *)view currentView:(UIView *)currentView pixels:(float)pixels; // 设置控件自动适配的height +(void)setSize:(UIView *)view currentView:(UIView *)currentView pixels:(float)pixels; // 自动适配控件拉伸比例(宽高相同) +(void)setSize:(UIView *)view currentView:(UIView *)currentView xPixels:(float)xPixels yPixels:(float) yPixels; // 自动适配控件拉伸比例(宽高不同) +(void)setTextFont:(UILabel *)view size:(float)size; //自动适配Label文字大小 +(void)setMutableWidth:(UIView *)view currentView:(NSDictionary *)currentView pixels:(float)pixels; // 批量自动适配控件宽度拉伸比例(宽高相同) +(void)setMutableSize:(UIView *)view currentView:(NSDictionary *)currentView xPixels:(float)xPixels yPixels:(float) yPixels; // 批量自动适配控件拉伸比例(宽高不同) +(void)setMutableTextFont:(NSDictionary *)currentView size:(float)size; // 批量自动适配Label文字大小 @end
<pre name="code" class="html">#import "Density.h" @implementation Density +(NSInteger)pixelsToDip:(float)pixels { return [[UIScreen mainScreen]bounds].size.width*pixels/320; } +(void)setWidth:(UIView *)view currentView:(UIView *)currentView pixels:(float)pixels { NSDictionary *childDis = NSDictionaryOfVariableBindings(currentView); NSInteger currentX =[Density pixelsToDip:pixels]; NSMutableDictionary *metrics = [[NSMutableDictionary alloc]init]; [metrics setObject:[NSString stringWithFormat:@"%ld",(long)currentX] forKey:@"currentX"]; [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[currentView(currentX)]" options:0 metrics:metrics views:childDis]]; } +(void)setHeight:(UIView *)view currentView:(UIView *)currentView pixels:(float)pixels { NSDictionary *childDis = NSDictionaryOfVariableBindings(currentView); NSInteger currentX =[Density pixelsToDip:pixels]; NSMutableDictionary *metrics = [[NSMutableDictionary alloc]init]; [metrics setObject:[NSString stringWithFormat:@"%ld",(long)currentX] forKey:@"currentX"]; [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[currentView(currentX)]" options:0 metrics:metrics views:childDis]]; } +(void)setSize:(UIView *)view currentView:(UIView *)currentView pixels:(float)pixels { NSDictionary *childDis = NSDictionaryOfVariableBindings(currentView); NSInteger currentX =[Density pixelsToDip:pixels]; NSMutableDictionary *metrics = [[NSMutableDictionary alloc]init]; [metrics setObject:[NSString stringWithFormat:@"%ld",(long)currentX] forKey:@"currentX"]; [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[currentView(currentX)]" options:0 metrics:metrics views:childDis]]; [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[currentView(currentX)]" options:0 metrics:metrics views:childDis]]; } +(void)setSize:(UIView *)view currentView:(UIView *)currentView xPixels:(float)xPixels yPixels:(float) yPixels { NSDictionary *childDis = NSDictionaryOfVariableBindings(currentView); NSInteger currentX =[Density pixelsToDip:xPixels]; NSInteger currentY =[Density pixelsToDip:yPixels]; NSMutableDictionary *metrics = [[NSMutableDictionary alloc]init]; [metrics setObject:[NSString stringWithFormat:@"%ld",(long)currentX] forKey:@"currentX"]; [metrics setObject:[NSString stringWithFormat:@"%ld",(long)currentY] forKey:@"currentY"]; [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[currentView(currentX)]" options:0 metrics:metrics views:childDis]]; [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[currentView(currentY)]" options:0 metrics:metrics views:childDis]]; } +(void)setTextFont:(UILabel *)view size:(float)size { view.font = [UIFont fontWithName:@"Helvetica-Bold" size:[Density pixelsToDip:size]]; } +(void)setMutableSize:(UIView *)view currentView:(NSDictionary *)currentView xPixels:(float)xPixels yPixels:(float) yPixels { NSInteger currentX =[Density pixelsToDip:xPixels]; NSInteger currentY =[Density pixelsToDip:yPixels]; NSMutableDictionary *metrics = [[NSMutableDictionary alloc]init]; [metrics setObject:[NSString stringWithFormat:@"%ld",(long)currentX] forKey:@"currentX"]; [metrics setObject:[NSString stringWithFormat:@"%ld",(long)currentY] forKey:@"currentY"]; for (NSString *key in currentView) { NSString *vfl = [NSString stringWithFormat:@"[%@(currentX)]",key]; NSString *vflY = [NSString stringWithFormat:@"V:[%@(currentX)]",key]; [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl options:0 metrics:metrics views:currentView]]; [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vflY options:0 metrics:metrics views:currentView]]; } } +(void)setMutableTextFont:(NSDictionary *)currentView size:(float)size { for (NSString *key in currentView) { UILabel *label = [currentView objectForKey:key]; label.font= [UIFont fontWithName:@"Helvetica-Bold" size:[Density pixelsToDip:size]]; } } +(void)setMutableWidth:(UIView *)view currentView:(NSDictionary *)currentView pixels:(float)pixels { NSInteger currentX =[Density pixelsToDip:pixels]; NSMutableDictionary *metrics = [[NSMutableDictionary alloc]init]; [metrics setObject:[NSString stringWithFormat:@"%ld",(long)currentX] forKey:@"currentX"]; for (NSString *key in currentView) { NSString *vfl = [NSString stringWithFormat:@"[%@(currentX)]",key]; [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl options:0 metrics:metrics views:currentView]]; } } @end
最后贴个使用
[Density setSize:self.view currentView:_logo xPixels:262 yPixels:82];