【问题标题】:UIButton titleEdgeInsets [duplicate]UIButton titleEdgeInsets [重复]
【发布时间】:2013-08-28 09:57:42
【问题描述】:

titleEdgeInsets 用于对齐按钮上的标题和图片。我在左边做了图像,在右边做了标题。但是当我点击按钮时,标题向左移动。谢谢你

UIImage *image = [UIImage imageNamed:imageName];
    [self setImage:image forState:UIControlStateNormal];

CGSize imageSize = self.imageView.frame.size;
CGSize titleSize = self.titleLabel.frame.size;

self.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
self.titleEdgeInsets = UIEdgeInsetsMake(0, ((self.frame.size.width-titleSize.width)/2)-imageSize.width, 0, 0);
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

【问题讨论】:

  • 会不会是因为你只为`UIControlStateNormal´设置了图片
  • 我发现边缘插入的行为非常违反直觉,所以我创建了一个小演示应用程序,您可以使用 github.com/tomas789/UIButtonEdgeInsets

标签: ios uibutton


【解决方案1】:

这是直接设置标题和图像的另一种方式----> :) 试试这个方法

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(0, 0, self.view.bounds.size.width, 40);
[aButton setImage:[UIImage imageNamed:@"imageName"] forState:UIControlStateNormal];
[aButton setTitle:@"Rate us on app store" forState:UIControlStateNormal];
[aButton setTitleEdgeInsets:UIEdgeInsetsMake(2, 50, 2, 20)];//set ur title insects also
[aButton setImageEdgeInsets:UIEdgeInsetsMake(2, -200, 2, 2)];//make negative edge for left side
[aButton setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:aButton];


【讨论】:

    猜你喜欢
    • 2017-04-01
    • 2013-03-04
    • 2011-06-01
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    • 2012-08-10
    • 2020-12-06
    • 1970-01-01
    相关资源
    最近更新 更多