UIButton titleEdgeInsets [重复]
Posted
技术标签:
【中文标题】UIButton titleEdgeInsets [重复]【英文标题】:UIButton titleEdgeInsets [duplicate] 【发布时间】: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 【参考方案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];
【讨论】:
以上是关于UIButton titleEdgeInsets [重复]的主要内容,如果未能解决你的问题,请参考以下文章
调整 titleEdgeInsets 和 imageEdgeInsets 时 UIButton 上的 title 和 text 之间的间距不正确