Façade 是一个 UIView
分类,允许您使用 现实世界的术语 构建您的 UI,就像您在与某人交谈时解释布局一样。它是一组便利方法,将大量的烦人并且乏味的框架代数封装成人类可读的格式。正因为如此,它足够 轻量级 和 灵活,可以允许您 快速轻松地构建外观漂亮的 UI,并基于设备方向或类型定义独特的布局体验。
[_headerImageView anchorTopCenterFillingWidthWithLeftAndRightPadding:0 topPadding:0 height:250];
NSArray *shareButtons = @[_facebookButton, _twitterButton, _googleButton, _pinterestButton];
[_headerImageView groupVertically:shareButtons centerRightWithRightPadding:7 spacing:7 width:45 height:45];
[_avatarImageView alignUnder:_headerImageView matchingCenterWithTopPadding:-50 width:100 height:100];
[_nameLabel alignUnder:_avatarImageView centeredFillingWidthWithLeftAndRightPadding:7 topPadding:14 height:28];
[_usernameLabel alignUnder:_nameLabel centeredFillingWidthWithLeftAndRightPadding:7 topPadding:2 height:19];
[_bioLabel sizeToFit];
[_bioLabel alignUnder:_usernameLabel centeredFillingWidthWithLeftAndRightPadding:15 topPadding:10 height:CGRectGetHeight(_bioLabel.frame)];
[_followButton alignUnder:_bioLabel matchingCenterWithTopPadding:20 width:250 height:44];
[_scrollView groupHorizontally:@[_messageButton, _statsButton, _addButton] centeredUnderView:_followButton topPadding:30 spacing:40 width:60 height:60];
[_messageLabel alignUnder:_messageButton matchingCenterWithTopPadding:7 width:80 height:13];
[_statsLabel alignUnder:_statsButton matchingCenterWithTopPadding:7 width:80 height:13];
[_addLabel alignUnder:_addButton matchingCenterWithTopPadding:7 width:80 height:13];
pod 'Facade'
添加到您的 Podfile 中,并运行 pod install
。UIView+Facade.h
。layoutSubviews
方法中,调用相应的 UIView+Facade
便利方法,以定义您希望您的 UI 如何布局。许多 UI 布局 从 将一个视图锚定到屏幕的顶部/角落开始,例如左上角有一个头像的个人资料视图。使用 Façade 做这种事情很容易 - 只需告诉视图将自身锚定到何处,并提供相应的填充/大小参数,然后它会为您完成剩下的工作。在 viewWillLayoutSubviews
中这样做还允许 Façade 在两种方向上保持该布局的一致性,如下所示
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
[self layoutFacade];
}
- (void)layoutFacade {
CGFloat padding = 10;
CGFloat size = 50;
[_redView anchorTopLeftWithLeftPadding:padding topPadding:padding width:size height:size];
[_orangeView anchorTopCenterWithTopPadding:padding width:size height:size];
[_yellowView anchorTopRightWithRightPadding:padding topPadding:padding width:size height:size];
[_greenView anchorCenterRightWithRightPadding:padding width:size height:size];
[_blueView anchorBottomRightWithRightPadding:padding bottomPadding:padding width:size height:size];
[_purpleView anchorBottomCenterWithBottomPadding:padding width:size height:size];
[_cyanView anchorBottomLeftWithLeftPadding:padding bottomPadding:padding width:size height:size];
[_brownView anchorCenterLeftWithLeftPadding:padding width:size height:size];
[_grayView anchorInCenterWithWidth:size height:size];
}
大多数布局从一个或多个锚点视图开始,其余组件通常相对于其他同级视图进行布局,并且它们的尺寸通常依赖于这些同级视图和父视图。我们已经整理了最常见的相对布局类型,以使此过程尽可能容易。下面是一些示例,显示了两种方向的结果
[_redView anchorTopLeftWithLeftPadding:10 topPadding:10 width:100 height:100];
[_orangeView alignToTheRightOf:_redView matchingTopAndFillingWidthWithLeftAndRightPadding:10 height:20];
[_greenView alignUnder:_orangeView matchingLeftAndFillingWidthWithRightPadding:10 topPadding:5 height:20];
[_blueView alignUnder:_greenView matchingLeftAndFillingWidthWithRightPadding:10 topPadding:5 height:50];
[_brownView alignUnder:_redView matchingLeftAndFillingWidthWithRightPadding:10 topPadding:10 height:30];
[_purpleView alignUnder:_brownView matchingRightWithTopPadding:10 width:100 height:40];
[_yellowView alignUnder:_purpleView centeredFillingWidthAndHeightWithLeftAndRightPadding:10 topAndBottomPadding:10];
您也可以将视图垂直或水平地放置在两个视图之间。
[_purpleView alignBetweenLeft:_orangeView andRight:_yellowView matchingTopWithLeftAndRightPadding:10 height:50];
[_brownView alignBetweenTop:_orangeView andBottom:_greenView matchingLeftWithTopAndBottomPadding:10 width:50];
[_grayView alignBetweenLeft:_greenView andRight:_blueView matchingTopWithLeftAndRightPadding:10 height:50];
[_cyanView alignBetweenTop:_yellowView andBottom:_blueView matchingLeftWithTopAndBottomPadding:10 width:50];
有时您希望将多个视图组件组合在一起,并使这个组合作为一个整体相对于另一个视图进行布局。一个常见的例子就是分享按钮——可能是在图片上方有Facebook、Pinterest、Twitter等分享图标。通过告诉相应的
[_redView groupVertically:@[_orangeView, _yellowView, _greenView] inUpperRightWithRightPadding:10 topPadding:10 spacing:10 width:50 height:50];
[self.view groupHorizontally:@[_blueView, _purpleView, _brownView] centeredUnderView:_redView topPadding:10 spacing:10 width:50 height:50];
这个功能的酷之处在于,它可以让您在不重新计算每个视图的框架的情况下添加、删除或重新组织视图到组中。
[_redView groupVertically:@[_orangeView, _yellowView] inUpperRightWithRightPadding:10 top:10 spacing:10 width:50 height:50];
[self.view groupHorizontally:@[_greenView, _blueView, _purpleView, _brownView, _blackView] centeredUnderView:_redView topPadding:10 spacing:10 width:50 height:50];
如果您想做出贡献,请开启一个pull request。请尽可能地保持与当前项目的风格/格式的一致性,并确保添加测试!
如果您有任何问题、功能请求等,我更愿意您创建一个issue而不是直接给我发邮件。
本项目基于MIT许可证提供。详情请参见LICENSE.txt
文件。