gpt4 book ai didi

ios - 如何将 UILabel 添加为 UIButton iOS 的 subview

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

我想在 UIButton 中添加一个 UILabel。这是我的代码:

- (void)viewDidLoad
{
[super viewDidLoad];
[[self addressSearch] setDelegate:self];
[[self worldMap] setDelegate:self];

self.boolPushButtonTapped = YES;
self.addressSearch.barStyle = 1;

//Create pushButton
self.pushButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.pushButton.frame = CGRectMake(106, 436, 110, 59);

[self.pushButton setBackgroundImage:[UIImage imageNamed:@"pushButton2.png"] forState:UIControlStateNormal];
[self.pushButton addTarget:self action:@selector(pushButtonTapped) forControlEvents:UIControlEventTouchUpInside];


//Label
self.distanceLabel = [[UILabel alloc] init];

self.distanceLabel.text = @"test";

[self.pushButton addSubview:self.distanceLabel];


[self.view addSubview:self.pushButton];
}

如果我不使用 alloc init 我会收到一个错误,如果我使用该标签中的文本则不会更改。错误:

*** Assertion failure in -[NSLayoutConstraint constant], /SourceCache/Foundation/Foundation-992/Layout.subproj/NSLayoutConstraint.m:560
2012-10-01 14:01:01.889 RemindMe[1116:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '(null)'

我想在这个按钮里面添加,因为有动画。

有什么建议吗?

最佳答案

你可以试试这个:

[self.pushButton.titleLabel setText: @"anytext"];

更新的答案:

UIButton Class Reference 中所述,您应该使用 setTitle:forState: 设置标题。

然后可以在按钮的关联标签对象 titleLabel 中完成格式化。

所以你不需要添加另一个 UILabel。

希望这对您有所帮助!

关于ios - 如何将 UILabel 添加为 UIButton iOS 的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12667390/

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