KeepLayout 项目教程
KeepLayoutMaking Auto Layout easier to code.项目地址:https://gitcode.com/gh_mirrors/ke/KeepLayout
1. 项目的目录结构及介绍
KeepLayout 项目的目录结构如下:
KeepLayout/
├── KeepLayout.podspec
├── LICENSE.md
├── README.md
├── UIViewController+KeepLayout.h
├── UIViewController+KeepLayout.m
├── UIView+KeepLayout.h
├── UIView+KeepLayout.m
├── KeepLayoutConstraint.h
├── KeepLayoutConstraint.m
├── KeepValue.h
├── KeepValue.m
├── NSArray+KeepLayout.h
├── NSArray+KeepLayout.m
├── KeepView.h
└── KeepView.m
目录结构介绍
KeepLayout.podspec
: 项目的 Podspec 文件,用于 CocoaPods 集成。LICENSE.md
: 项目的许可证文件,采用 MIT 许可证。README.md
: 项目的说明文档。UIViewController+KeepLayout.h
和UIViewController+KeepLayout.m
: 扩展 UIViewController,添加 KeepLayout 功能。UIView+KeepLayout.h
和UIView+KeepLayout.m
: 扩展 UIView,添加 KeepLayout 功能。KeepLayoutConstraint.h
和KeepLayoutConstraint.m
: 自定义 NSLayoutConstraint 子类,用于 KeepLayout。KeepValue.h
和KeepValue.m
: 定义 KeepValue 类型,用于布局属性。NSArray+KeepLayout.h
和NSArray+KeepLayout.m
: 扩展 NSArray,添加 KeepLayout 功能。KeepView.h
和KeepView.m
: 提供 KeepLayout 的视图相关功能。
2. 项目的启动文件介绍
KeepLayout 项目的启动文件是 UIViewController+KeepLayout.h
和 UIViewController+KeepLayout.m
,以及 UIView+KeepLayout.h
和 UIView+KeepLayout.m
。这些文件通过类别(Category)扩展了 UIViewController 和 UIView,添加了 KeepLayout 功能。
UIViewController+KeepLayout.h
#import <UIKit/UIKit.h>
@interface UIViewController (KeepLayout)
@property (readonly) UIView *keepLayoutView;
@end
UIViewController+KeepLayout.m
#import "UIViewController+KeepLayout.h"
#import "KeepLayoutConstraint.h"
@implementation UIViewController (KeepLayout)
- (UIView *)keepLayoutView {
UIView *layoutView = objc_getAssociatedObject(self, @selector(keepLayoutView));
if (!layoutView) {
layoutView = [[UIView alloc] initWithFrame:CGRectZero];
layoutView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:layoutView];
// 添加约束
[self.view addConstraints:@[
[KeepLayoutConstraint constraintWithItem:layoutView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1 constant:0],
[KeepLayoutConstraint constraintWithItem:layoutView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.bottomLayoutGuide attribute:NSLayoutAttributeTop multiplier:1 constant:0],
[KeepLayoutConstraint constraintWithItem:layoutView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:0],
[KeepLayoutConstraint constraintWithItem:layoutView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:0]
]];
objc_setAssociatedObject(self, @selector(keepLayoutView), layoutView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return layoutView;
}
@end
UIView+KeepLayout.h
#import <UIKit/UIKit.h>
@interface UIView (KeepLayout)
@property (readonly) KeepAttribute *keepWidth;
@property (readonly) KeepAttribute *keepHeight;
@property (readonly) KeepAttribute *(^keepTopOffsetTo)(UIView *);
@property (readonly) KeepAttribute *(^keepLeftOffsetTo)(UIView *);
@property (readonly) KeepAttribute *(^keepBottomOffsetTo)(UIView *);
KeepLayoutMaking Auto Layout easier to code.项目地址:https://gitcode.com/gh_mirrors/ke/KeepLayout
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考