Mac_NSLayout

使用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]];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值