KeepLayout 项目教程

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.hUIViewController+KeepLayout.m: 扩展 UIViewController,添加 KeepLayout 功能。
  • UIView+KeepLayout.hUIView+KeepLayout.m: 扩展 UIView,添加 KeepLayout 功能。
  • KeepLayoutConstraint.hKeepLayoutConstraint.m: 自定义 NSLayoutConstraint 子类,用于 KeepLayout。
  • KeepValue.hKeepValue.m: 定义 KeepValue 类型,用于布局属性。
  • NSArray+KeepLayout.hNSArray+KeepLayout.m: 扩展 NSArray,添加 KeepLayout 功能。
  • KeepView.hKeepView.m: 提供 KeepLayout 的视图相关功能。

2. 项目的启动文件介绍

KeepLayout 项目的启动文件是 UIViewController+KeepLayout.hUIViewController+KeepLayout.m,以及 UIView+KeepLayout.hUIView+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),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

任铃冰Flourishing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值