如何使用自动布局在 ios 中以编程方式在 UIview 上添加 UIbutton

Posted

技术标签:

【中文标题】如何使用自动布局在 ios 中以编程方式在 UIview 上添加 UIbutton【英文标题】:How to add UIbutton on UIview programmatically in ios using autolayouts 【发布时间】:2015-07-14 10:20:12 【问题描述】:

我想使用自动布局以编程方式在 UIview 上添加 uibutton 并尝试了一些代码,但它不起作用请帮帮我

//使用自动布局添加UIview

UIView * myView;
myView = [UIView new];
    myView.translatesAutoresizingMaskIntoConstraints = NO;
    myView.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
    [self.view addSubview:myView];

  NSLayoutConstraint * constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:30.0f];
    [self.view addConstraint:constraint];

    constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:100.0f];
    [self.view addConstraint:constraint];

    constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:100.0f];
    [self.view addConstraint:constraint];

    constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:100.0f];
    [self.view addConstraint:constraint];

使用自动布局在 uiview 上添加按钮:-

mainButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [mainButton setTitle:@"MainButton" forState:UIControlStateNormal];
    [mainButton sizeToFit];
    mainButton.backgroundColor = [UIColor blackColor];
    mainButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:mainButton];

 NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:myview attribute:NSLayoutAttributeLeading multiplier:1.0f constant:30.f];
    [self.view addConstraint:constraint];

    constraint = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:myview attribute:NSLayoutAttributeTop multiplier:1.0f constant:30.f];
    [self.view addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant:20.f];
    [self.view addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:20.f];
    [self.view addConstraint:constraint];

但是 myview 上没有添加按钮,请帮帮我

【问题讨论】:

【参考方案1】:

这是有效的:

UIView *myView = [UIView new];
myView.translatesAutoresizingMaskIntoConstraints = NO;
myView.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
[self.view addSubview:myView];

NSLayoutConstraint * constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:30.0f];
[self.view addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:100.0f];
[self.view addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:140.0f];
[self.view addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:100.0f];
[self.view addConstraint:constraint];

UIButton *mainButton = [UIButton new];

mainButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[mainButton setTitle:@"MainButton" forState:UIControlStateNormal];
[mainButton sizeToFit];
mainButton.backgroundColor = [UIColor blackColor];
mainButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:mainButton];

NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:30.f];
[self.view addConstraint:constraint1];

constraint1 = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeTop multiplier:1.0f constant:30.f];
[self.view addConstraint:constraint1];

constraint1 = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant:20.f];
[self.view addConstraint:constraint1];

constraint1 = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:20.f];
[self.view addConstraint:constraint1];

【讨论】:

【参考方案2】:

您还没有为按钮定义高度限制,您已经定义了两次宽度限制。

【讨论】:

以上是关于如何使用自动布局在 ios 中以编程方式在 UIview 上添加 UIbutton的主要内容,如果未能解决你的问题,请参考以下文章

如何在ios swift中以编程方式创建自动布局等宽度约束?

如何在标准 UITableViewCell 中以编程方式自动布局右侧的自定义 UIButton。

如何在 ios 中以编程方式添加约束

如何在自动布局中以编程方式执行 uilabel

有啥方法可以在 Swift 中以编程方式获取所有应用的自动布局约束

在 iOS 8 中以编程方式将视图中的按钮居中使用约束