mac 相对布局

来源:互联网 发布:apache ab post请求 编辑:程序博客网 时间:2024/06/05 05:45
#import "NSView+TWFrame.h"@implementation NSView (TWFrame)- (void)setX:(CGFloat)x{    CGRect frame = self.frame;    frame.origin.x = x;    self.frame = frame;}- (CGFloat)x{    return self.frame.origin.x;}- (void)setY:(CGFloat)y{    CGRect frame = self.frame;    frame.origin.y = y;    self.frame = frame;}- (CGFloat)y{    return self.frame.origin.y;}// 返回高度-(CGFloat)height {    return  self.frame.size.height;}- (void)setHeight:(CGFloat)newheight{    CGRect newframe = self.frame;    newframe.size.height = newheight;    self.frame = newframe;}//  返回宽度- (CGFloat)width{    return self.frame.size.width;}//  设置宽度- (void)setWidth:(CGFloat)newwidth{    CGRect newframe = self.frame;    newframe.size.width = newwidth;    self.frame = newframe;}//  返回Y- (CGFloat)top{    return self.frame.origin.y;}- (void)setTop:(CGFloat)newtop{    CGRect newframe = self.frame;    newframe.origin.y = newtop;    self.frame = newframe;}//  返回X- (CGFloat)left{    return self.frame.origin.x;}- (void)setLeft:(CGFloat) newleft{    CGRect newframe = self.frame;    newframe.origin.x = newleft;    self.frame = newframe;}//  返回Y + Height- (CGFloat)bottom{    return self.frame.origin.y + self.frame.size.height;}- (void)setBottom:(CGFloat)newbottom{    CGRect newframe = self.frame;    newframe.origin.y = newbottom - self.frame.size.height;    self.frame = newframe;}//  返回X + width- (CGFloat)right{    return self.frame.origin.x + self.frame.size.width;}- (void)setRight:(CGFloat)newright{    CGFloat delta = newright - (self.frame.origin.x + self.frame.size.width);    CGRect newframe = self.frame;    newframe.origin.x += delta ;    self.frame = newframe;}- (void)setSize:(CGSize)size{    CGRect frame = self.frame;    frame.size = size;    self.frame = frame;}- (CGSize)size{    return self.frame.size;}@end

使用autolayout

UIView *purpleView = [[UIView alloc] init];    purpleView.backgroundColor = [UIColor purpleColor];    // 禁止将 AutoresizingMask 转换为 Constraints    purpleView.translatesAutoresizingMaskIntoConstraints = NO;    [self.view addSubview:purpleView];    // 添加 width 约束    NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:purpleView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:150];    [purpleView addConstraint:widthConstraint];    // 添加 height 约束    NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:purpleView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:150];    [purpleView addConstraint:heightConstraint];    // 添加 left 约束    NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:purpleView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:100];    [self.view addConstraint:leftConstraint];    // 添加 top 约束    NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:purpleView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:200];    [self.view addConstraint:topConstraint];