我希望按钮文本在x维度中出现在按钮中间附近。然而,按钮文字仍保留在视图的最左侧,就像x
为0
一样。我确保setTitleEdgeInsets
和setContentHorizontalAlignment
。我还尝试设置contentEdgeInsets
而不是titleEdgeInsets
。我错过了什么?
UIButton *row = [[UIButton alloc] initWithFrame:
CGRectMake(0, index * kRowHeight, 320, kRowHeight)];
[row setTitle:value forState:UIControlStateNormal];
[row setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[row setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[row setTitleEdgeInsets:UIEdgeInsetsMake(180.0, 20.0, 0, 0)];
[self.view addSubview:row];
答案 0 :(得分:2)
我错误地订购了参数。
UIEdgeInsets
定义为
typedef struct {
CGFloat top, left, bottom, right;
} UIEdgeInsets;
答案 1 :(得分:1)
该按钮具有titleLabel
属性。更改文本对齐方式。
答案 2 :(得分:0)
您是否尝试使用edgeInsets 20 20 20 20?
答案 3 :(得分:0)
您好为Inset Button添加此代码
extension UIButton {
/**
Creates an edge inset for a button
:param: top CGFLoat
:param: left CGFLoat
:param: bottom CGFLoat
:param: right CGFLoat
*/
func setInset(#top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat) {
self.titleEdgeInsets = UIEdgeInsetsMake(top, left, bottom, right)
}
}