微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何以编程方式设置UIButton – iOS的垂直对齐方式

我知道如何设置按钮与IB的垂直对齐方式;看到 here.但是我不能以编程方式来做…这是我尝试过的一件事:
UIButton *button  = [[UIButton alloc] initWithFrame:CGRectMake(10,10,110,130)];
[button setTitle:@"Align" forState:UIControlStatenormal];
button.backgroundColor = [UIColor whiteColor];
button.titleLabel.textColor = [UIColor grayColor];
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];

[self.view addSubview:button];

[button release];

我也试过插图…

但是对齐不会改变.

请注意,我想保留按钮的CustomStyle(我不想要认的外观和感觉).

解决方法

您可以以这种方式对齐(水平和垂直)按钮的文本:
UIButton *button  = [[UIButton alloc] initWithFrame:CGRectMake(0,200,70)];
        button.backgroundColor = [UIColor whiteColor];
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        button.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
        [button setTitle:@"Align" forState:UIControlStatenormal];
        [self.container addSubview:button];

原文地址:https://www.jb51.cc/iOS/337424.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐