auto Layout

1, 以iPhone5s为根基的代码自动布局后续版本全适配.

扩展链接(关于iPhone的各版本像素点以及iOS8,Xcode6的一些描述):

http://www.cocoachina.com/ios/20140912/9601.html

(1)引入数据

iPhone4s      480/320 ———  1.5

iPhone5s      568/320  ———1.775

iPhone6        667/375  ——— 1.779

iPhone6plus  736/414  ———1.778

观察可以发现规律,排除iPhone4s ,后续的iPhone版本高宽比例大致都处在1.77 - 1.78这个范围.(就目前而言).于是就产生了以iPhone5s为根基的代码自动布局后续版本全适配.

(2)关键代码段

AppDelegate.h
 1 #import <UIKit/UIKit.h>
 2
 3 @interface AppDelegate : UIResponder <UIApplicationDelegate>
 4
 5 @property (strong, nonatomic) UIWindow *window;
 6
 7 @property float autoSizeScaleX;
 8 @property float autoSizeScaleY;
 9
10 + (void)storyBoradAutoLay:(UIView *)allView;
11 @end
 1 #define ScreenHeight ([[UIScreen mainScreen] bounds].size.height)
 2
 3 #define ScreenWidth ([[UIScreen mainScreen] bounds].size.width)
 4
 5 #import "AppDelegate.h"
 6
 7 @interface AppDelegate ()
 8
 9 @end
10
11 @implementation AppDelegate
12 //此处的类方法实现需要注意,如果当前的view层次结构比较复杂,则应该多嵌套几次循环
13 + (void)storyBoradAutoLay:(UIView *)allView
14 {
15     for (UIView *temp in allView.subviews) {
16         temp.frame = CGRectMake1(temp.frame.origin.x, temp.frame.origin.y, temp.frame.size.width, temp.frame.size.height);
17         for (UIView *temp1 in temp.subviews) {
18             temp1.frame = CGRectMake1(temp1.frame.origin.x, temp1.frame.origin.y, temp1.frame.size.width, temp1.frame.size.height);
19         }
20     }
21 }
22
23 CG_INLINE CGRect
24 CGRectMake1(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
25 {
26     AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
27     CGRect rect;
28     rect.origin.x = x * myDelegate.autoSizeScaleX; rect.origin.y = y * myDelegate.autoSizeScaleY;
29     rect.size.width = width * myDelegate.autoSizeScaleX; rect.size.height = height * myDelegate.autoSizeScaleY;
30     return rect;
31 }
32
33 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
34     // Override point for customization after application launch.
35
36     AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
37
38         myDelegate.autoSizeScaleX = ScreenWidth/320;
39         myDelegate.autoSizeScaleY = ScreenHeight/568;
40
41
42     return YES;
43 }

(3)使用时只要在有需求的.m文件导入Appdelegate.h,引用storyBoradAutoLay:类方法即可

    [AppDelegate storyBoradAutoLay:self.view];
时间: 2024-08-10 19:18:13

auto Layout的相关文章

Auto Layout Guide----(三)-----Anatomy of a Constraint

Anatomy of a Constraint 剖析约束 The layout of your view hierarchy is defined as a series of linear equations. Each constraint represents a single equation. Your goal is to declare a series of equations that has one and only one possible solution. A samp

Auto Layout 使用心得

此系列文章代码仓库在 https://github.com/johnlui/AutoLayout ,有不明白的地方可以参考我的 Auto Layout 设置哦,下载到本地打开就可以了. 简介 Auto Layout 是苹果在 Xcode 5 (iOS 6) 中新引入的布局方式,旨在解决 3.5 寸和 4 寸屏幕的适配问题.屏幕适配工作在 iPhone 6 及 plus 发布以后变得更加重要,而且以往的“笨办法”的工作量大幅增加,所以很多人开始学习使用 Auto Layout 技术. 初体验 0.

Day3 : Auto layout 和 JVFloatLabeledTextfield框架 学习笔记

为了可以优化项目的UI,为了可以使用JVFloatLabeledTextfield框架来构建文本输入框(动画效果超赞),今天重点学习了Auto Layout(以下简称AL)技术,主要是了解AL的工作原理,并且要掌握用代码添加constraints. 1.JVFloatLabeledTextfield JVFloatLabeledTextfield框架可以让文本框呈现一个漂浮的PlaceHolder,简洁.明确.生动.而这个框架另一个让我大开眼界的是他利用AL技术画直线,做出一个简单的表单页面.画

iOS开发之Auto Layout入门

随着iPhone6与iOS8的临近,适配的问题讲更加复杂,最近学习了一下Auto Layout的使用,与大家分享.  什么是Auto Layout? Auto Layout是iOS6发布后引入的一个全新的布局特性,其目的是弥补以往Autoresizing在布局方面的不足之处,以及未来面对更多尺寸适配时界面布局可以更好的适应. 为什么要用Auto Layout? Autolayout能解决不同屏幕(iPhone4,iPhone5,iPad...)之间的适配问题. 在iPhone4时代开发者只需要适

Auto Layout Guide----(二)-----Auto Layout Without Constraints

Auto Layout Without Constraints 没有约束的自动布局 Stack views provide an easy way to leverage the power of Auto Layout without introducing the complexity of constraints. A single stack view defines a row or column of user interface elements. The stack view a

【转】有了Auto Layout,为什么你还是害怕写UITabelView的自适应布局?

Apple 算是最重视应用开发体验的公司了.从Xib到StoryBoard,从Auto Layout到Size Class,每一次的更新,都会给iOS应用的开发带来不小的便利.但是,对于绝对多数iOS攻城狮来说,我们依然还是很害怕写UITabelVIew的自适应布局.当然,害怕不是因为我们不会写,或者本身有什么特殊的技术点,而是因为太麻烦.当然,文章的后半部分,会给出相应的解决方案,毕竟本文不是为了吐槽而吐槽. UITabelView的自适应布局有多麻烦? 数据类型的不确定性:种类越多,页面越复

【Auto Layout】Xcode6创建Auto Layout 约束时产生的一些变化【iOS开发教程】

[#Auto Layout#]Xcode6创建Auto Layout 约束时产生的一些变化 ? ? ? 运行效果: 没有从顶部开始,似乎是从statusbar的20高度以外开始计算的 ? ? ? ? ? 另外在设置顶部约束和底部约束时也尽量不要选择默认的,尽量点击右侧的小箭头,在弹框中选择父视图,如下图所示: ? ? Created: 05/24/2015Link:?http://www.cnblogs.com/ChenYilong/p/4526893.html

iOS 使用LayoutGuide 来限制控件的位置,配合Auto Layout constraints

UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; [self.view addSubview:button]; [button setTranslatesAutoresizingMaskIntoConstraints: NO]; // 得到当前视图的最低基准限制,这个是对于Auto Layout constraint来说的. id bottomGuide = self.bottomLayoutGu

iOS屏幕适配方案-Auto Layout

市场上的android手机五花八门.各种尺寸的屏幕让android程序员们比較头疼. 也有一些大神写了一些博客提出了自己的观点.iOS貌似也迎来了大屏6+,因此屏幕适配的问题也是有滴,因此苹果也有自己的方法-auto Layout . 本人初学iOS.今天学了自己主动布局.在学习的过程中,毕竟还是有些知识点没有接触到的,因此写这篇博客来深入的了解一下Auto Layout. 官方解释: Auto Layout 是一个系统,能够让你通过创建元素之间关系的数学描写叙述来布局应用程序的用户界面.--<

【转 iOS 8 Auto Layout界面自动布局系列2-使用Xcode的Interface Builder添加布局约束

原文网址:http://blog.csdn.net/pucker/article/details/41843511 上一篇文章<iOS 8界面自动布局系列-1>简要介绍了iOS界面布局方式的前世今生.本篇文章将详细介绍如何使用自动布局实现不同屏幕尺寸的适配. 添加自动布局约束(下文简称约束)有以下三种方式: 使用Xcode的Interface Builder界面设计器添加并设置约束 通过代码逐条添加约束 通过可视化格式语言VFL添加约束 本文将以一个简单的例子来演示如何使用这几种方式添加约束,