我知道如何设置按钮与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];