UIButton titleEdgeInsets [英] UIButton titleEdgeInsets
本文介绍了UIButton titleEdgeInsets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
titleEdgeInsets用于对齐按钮上的标题和图片。我在左边做了图像,在右边做了标题。但是当我点击按钮时,标题向左移动。谢谢
titleEdgeInsets used to align the title and the picture on the button. I did image on the left and title on the right.But when I click on the button, the title is shifting to the left. Thank you
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;
推荐答案
这是直接设置标题和图像的另一种方式----> :)
试试这种方式,
This is the example another way for setting the title and image in straight ----> :)
try this way,
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文