KVConstraintExtensionsMaster
使自动布局约束
的使用更容易。它提供简单、可读性更强、更丰富的代码重用性和强大的API,通过布局属性创建新约束、访问和修改现有约束。
KVConstraintExtensionsMaster
的主要目标是减少与NSLayoutConstraint
一起工作时产生响应式UI(用户界面)设计的开发人员开销。
要使用CocoaPods将KVConstraintExtensionsMaster集成到Xcode项目中,请简单地将以下行添加到您的Podfile
pod "KVConstraintExtensionsMaster"
如果您正在使用Swift,请确保在您的
Podfile
中添加use_frameworks!
并将您的目标设置为iOS 8+
platform :ios, '8.0'
use_frameworks!
pod "KVConstraintExtensionsMaster"
然后,请在终端中运行以下命令
$ pod install
在您从CocoaPods安装了任何东西后,您应该打开{Project}.xcworkspace
而不是{Project}.xcodeproj
$ open *.xcworkspace
然后,运行以下命令以构建KVConstraintExtensionsMaster
框架
$ carthage update
现在将构建的KVConstraintExtensionsMaster.framework
拖入您的Xcode项目。
手动
#import "KVConstraintExtensionsMaster.h"
如果您正在使用Swift,您必须在
{Project}-Bridging-Head.h
头文件中导入KVConstraintExtensionsMaster.h
头文件。
+Digits
[x] 不需要使用更多的程序化
还是通过Interface Builder
在视图上应用的。
[x] 您可以轻松添加那些在StoryBoard/xib中不可能或很难添加的约束
。
[x] 由于约束是 累积的
且不会相互覆盖。如果您已经有一个现有的约束,设置同样类型的另一个约束不会覆盖它。因此,该库只会添加任何约束一次,所以您不需要担心我们多次添加相同的约束会发生什么。
[x] 可以轻松地 修改
任何约束,并用新约束 替换
它。
[x] 可以通过精美的动画轻松地 修改
任何约束。
[x] 在 iOS 7、8、9 和更高版本中,您还可以轻松地添加 比例/比率
约束。
[x] 您只需编写几行代码即可 添加多个
约束。
[x] 该库的所有方法都有前缀 prepare
或 apply
,根据其行为。因此,您不需要记住所有方法,只需输入方法前缀,然后 编译器会自动
提供方法建议。
带有前缀 prepare
的方法用于准备约束或准备视图以便应用约束。带有前缀 apply
的方法用于向适当的视图应用/添加约束。
## ------: Just Two steps to Apply\Add constraints by programatically :------
步骤 1
首先创建和配置约束的视图层次结构。
步骤 2
根据选定的视图,通过调用带有前缀 apply
的 KVConstraintExtensionsMaster 库方法来应用约束。
@interface ViewController ()
@property (strong, nonatomic) UIView *containerView;// may be any view like /*containerView*/
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createAndConfigureViewHierarchy]; // Step 1
[self applyConstraint]; // Step 2
}
- (void)createAndConfigureViewHierarchy
{
self.containerView = [UIView prepareAutoLayoutView];
self.containerView.backgroundColor = [UIColor Purple];
[self.view addSubview:self.containerView];
}
-(void)applyConstraint
{
// Here we are going to apply constraints
[self.containerView applyLeadingPinConstraintToSuperview:20];
[self.containerView applyTrailingPinConstraintToSuperview:20];
// we can also apply leading and trailing of containerView both by using the below method.
But this method is only useful when both leading and trailing have same pading.
// [self.containerView applyLeadingAndTrailingPinConstraintToSuperview:0];
[self.containerView applyTopPinConstraintToSuperview:65.0f];
[self.containerView applyBottomPinConstraintToSuperview:50.0f];
}
@end
Keshav Vishwkarma, [email protected]
KVConstraintExtensionsMaster 以 MIT 许可证提供。有关更多信息,请参阅 LICENSE 文件。