MMLayout
今天想介绍的就是开发中,布局的问题. 本人一直使用的传统的Frame
来写UI
个人习惯,我觉得Frame 的效率还是比autolayout
要高的,这个小伙伴可以测试一下的.
但是使用Frame 的时候 代码计算的时候很多,比如距离左边的控件的最大x值是多少,这样写来写去,实在代码可以不方便看,然后还的自己计算,很麻烦.
下面是简单的调用 下面的使用时在UIViewController 里面添加UI使用
-(void)viewDidLayoutSubviews{
[self.yellowView make_Layout:^(MMLayout *layout) {
layout.width = 100;
layout.height = 200;
layout.left = 5;
layout.top = 100;
}];
__weak typeof(self)_self = self;
[self.yellowViewTwo make_Layout:^(MMLayout *layout) {
__strong typeof(_self)self = _self;
layout.left = self.yellowView.maxX + 5;
layout.top = self.yellowView.y;
layout.size = self.yellowView.bounds.size;
}];
[self.yellowViewTree make_Layout:^(MMLayout *layout) {
layout.height = 50;
layout.width = 100;
layout.right = 10;
layout.top = 10;
}];
}
链接式写Frame
1.创建一个View
UIView *newView = [UIView new];
[self.view addSubViews:newView];
newView.height(100).left(10).width(100).top(100);
相同设置
UIView *redView = [UIView new];
[self.view addSubview:redView];
UIView *redView1 = [UIView new];
[self.view addSubview:redView1];
UIView *redView2 = [UIView new];
[self.view addSubview:redView2];
UIView *redView3 = [UIView new];
[self.view addSubview:redView3];
redView.m_left(10).m_top(10).m_size(CGSize(50,50));
redView1.m_equalToTop(redView).m_equalToSize(redView).m_left(redView.mm_maxX + 10);
redView2.m_equalToTop(redView1).m_equalToSize(redView1).m_left(redView1.mm_maxX + 10);
redView3.m_equalToTop(redView2).m_equalToSize(redView2).m_left(redView2.mm_maxX + 10);
以上是两种方式代码阅读我个人觉得很好阅读
平时在控制器写UI代码的时候布局的代码 我一般都会写在 - (void)viewDidLayoutSubviews
这个方法当控制器的View发生变化的时候会来到这个方法,比如一个场景,就是横竖屏的时候,你不用监听屏幕的旋转,只要在这里写上相对好的布局,横竖屏的时候会自己调用这个方法,从新的计算子控件的位置.
假设你在自定义View的时候在View的里面写布局的方法是 layoutSubviews
这个方法 父控件发生变化会调用LayoutSubViews
个人觉得,如果你用代码写aotolayout的话,有的时候更新约束,也是很麻烦的一件事.然后调试的时候,也会出现各种aotolayout的错误提示.
使用frame来写,避免了aotolayout的错误,错误率降低了.
话又说回来,autoLaout 确实很强大. 比如写UILabel
的时候很方便,用frame写的话,还得计算text的size. 不过开发中,使用某一个东西,当然是有利有弊.这个还的根据大家的喜好,和公司的规范.来开发.
谢谢大家.