NSLayoutConstraint

2024-01-27 00:38
文章标签 nslayoutconstraint

本文主要是介绍NSLayoutConstraint,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

NSLayoutConstraint 

使用下面代码测试自动布局使用程序写

增加测试UIButton代码:

UIButton* onHelpView = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [onHelpView setTitle:@"mgen" forState:UIControlStateNormal];

    onHelpView.backgroundColor = [UIColorgreenColor];

    [self.view addSubview:onHelpView];

    //禁止自动转换AutoresizingMask

    onHelpView.translatesAutoresizingMaskIntoConstraints =NO;

    [self.view addSubview:onHelpView];

    使用NSLayoutConstraint

    NSMutableArray *array = [NSMutableArrayarrayWithArray: [NSLayoutConstraintconstraintsWithVisualFormat:@"|[onHelpView]|"

                                                                                                   options:0

                                                                                                   metrics:nil

                                                                                                     views:NSDictionaryOfVariableBindings(onHelpView)]];


    [array addObjectsFromArray:[NSLayoutConstraintconstraintsWithVisualFormat:@"V:|[onHelpView]|"

                                                                      options:0

                                                                      metrics:nil

                                                                        views:NSDictionaryOfVariableBindings(onHelpView)]];

    [self.view addConstraints:array];


另一种写法

    [self.viewaddConstraint:[NSLayoutConstraint

                              constraintWithItem:onHelpView

                              attribute:NSLayoutAttributeLeft

                              relatedBy:NSLayoutRelationEqual

                              toItem:self.view

                              attribute:NSLayoutAttributeLeft

                              multiplier:1

                              constant:0]];

    

    [self.viewaddConstraint:[NSLayoutConstraint

                              constraintWithItem:onHelpView

                              attribute:NSLayoutAttributeTop

                              relatedBy:NSLayoutRelationEqual

                              toItem:self.view

                              attribute:NSLayoutAttributeTop

                              multiplier:1

                              constant:0]];

    

    [self.viewaddConstraint:[NSLayoutConstraint

                              constraintWithItem:onHelpView

                              attribute:NSLayoutAttributeWidth

                              relatedBy:NSLayoutRelationEqual

                              toItem:self.view

                              attribute:NSLayoutAttributeWidth

                              multiplier:1

                              constant:0]];

    

    //定义高度是父View的三分之一

    [self.view addConstraint:[NSLayoutConstraint

                              constraintWithItem:onHelpView

                              attribute:NSLayoutAttributeHeight

                              relatedBy:NSLayoutRelationEqual

                              toItem:self.view

                              attribute:NSLayoutAttributeHeight

                              multiplier:1

                              constant:0]];

两种效果相同。


再加上KVO测试代码

[onHelpView addObserver:self forKeyPath:@"bounds" options:NSKeyValueObservingOptionNew |NSKeyValueObservingOptionInitial context:nil];

//KVO回调

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object == onHelpView && [keyPath isEqualToString:@"bounds"])
    {[onHelpView setTitle:NSStringFromCGSize(onHelpView.bounds.size) forState:UIControlStateNormal];

}

转载传送门:http://blog.csdn.net/djl4104804/article/details/20768901

这篇关于NSLayoutConstraint的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/648535

相关文章

swift中NSLayoutConstraint布局的使用

1.第一步,创建试图,添加到self.view中 var button1: UIButton? = UIButton(frame: CGRectMake(350, 20, 100, 50))button1!.backgroundColor = UIColor(white:5, alpha: 0.5)<span style="white-space:pre"> </span>se

Could not instantiate class named NSLayoutConstraint

xcode 升级到了 4.5 之后 只要往xib上面拖放控件 就会报错, 解决方法, 选中当前的 xib文件, 再右边的 inspector 一栏 将 interface builder document 下的 use autolayout 复选框去掉就可以了 VIA:http://www.lazyid.com/2012/07/04/could-not-instantiate-c