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

前端之家收集整理的这篇文章主要介绍了如何以编程方式设置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.f2er.com/iOS/337424.html

猜你在找的iOS相关文章