如何以编程方式设置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];

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...