使用NSLayout对控件进行布局,这个如果放到iOS中我是有自己写的一套控件布局的工具类。好多人都在用第三方的masry但是我没有选择用,因为第三方库中会有许多自己用不到的东西。如果出现问题有时候也不好处理。在mac os开发中使用NSLayout也是一次自己的尝试。
我这里有三个 nsview。布局如下
- (void)addTowButton {
self.towButton = [[NSView alloc]init];
[self.towButton setWantsLayer:YES];
self.towButton.layer.backgroundColor = [NSColor purpleColor].CGColor;
// 禁止将 AutoresizingMask转换为Constraints
self.towButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.window.contentView addSubview:self.towButton];
// 添加width约束
NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:self.towButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:150];
[self.towButton addConstraint:width];
// 添加height约束
NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:self.towButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:150];
[self.towButton addConstraint:height];
// 添加left约束
NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:self.towButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.window.contentView attribute:(NSLayoutAttributeLeft) multiplier:1.0 constant:100];
[self.window.contentView addConstraint:left];
// 添加top约束
NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self.towButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.window.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:30];
[self.window.contentView addConstraint:top];
}
- (void)addAnother{
self.anotherButton = [[NSView alloc]init];
[self.anotherButton setWantsLayer:YES];
self.anotherButton.layer.backgroundColor = [NSColor redColor].CGColor;
self.anotherButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.window.contentView addSubview:self.anotherButton];
// 设置约束
// 设置宽度
NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:self.anotherButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:150];
[self.anotherButton addConstraint:width];
// 设置高度
NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:self.anotherButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:150];
[self.anotherButton addConstraint:height];
// 设置左侧
NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:self.anotherButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.towButton attribute:NSLayoutAttributeRight multiplier:1.0 constant:100];
[self.window.contentView addConstraint:left];
// 设置top
NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self.anotherButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.window.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:50];
[self.window.contentView addConstraint:top];
}
- (void)addLowLayout {
self.lowButton = [[NSView alloc]init];
[self.lowButton setWantsLayer:YES];
self.lowButton.layer.backgroundColor = [NSColor greenColor].CGColor;
self.lowButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.window.contentView addSubview:self.lowButton];
// align layer from the width
NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:self.lowButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:self.window.frame.size.width / 3];
[self.lowButton addConstraint:width];
// align layer from the height
[self.lowButton addConstraint:[NSLayoutConstraint constraintWithItem:self.lowButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:self.window.frame.size.height / 3]];
// align layer from the left
[self.window.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.lowButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.window.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:100]];
// align layer from the top
[self.window.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.lowButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.towButton attribute:NSLayoutAttributeBottom multiplier:1.0 constant:100]];
}