gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:58:57 24 4
gpt4 key购买 nike

我想使用自动布局以编程方式在 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];

但是我的 View 上没有添加按钮请帮我一些

最佳答案

这是有效的:

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

enter image description here

关于ios - 如何使用自动布局在 ios 中以编程方式在 UIview 上添加 UIbutton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31403896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com