UIEdgeInsets不起作用

时间:2013-07-26 20:30:14

标签: ios

我希望按钮文本在x维度中出现在按钮中间附近。然而,按钮文字仍保留在视图的最左侧,就像x0一样。我确保setTitleEdgeInsetssetContentHorizontalAlignment。我还尝试设置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];

4 个答案:

答案 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)
    }
}