IOS - UIButton文本左对齐等于另一个UIButton的文本包含图像

时间:2015-12-15 09:26:35

标签: ios objective-c uibutton

我是IOS的新手。我想知道如何使Contact.store = function(email, cb) { var server1 = Contact.app.dataSources.Server1; var server2 = Contact.app.dataSources.Server2; server1.checkEmail(email, function(validated) { if(validated) { server2.getDetails(email, function(details) { if(details) { server1.save(details, function(success) { if(success) { cb(null, details); } }) } }) } }) } 左对齐的文字等于包含图片的另一个UIButton的文字。
哪种灵活的方式实现呢? (我的意思是灵活的方式是当我改变图像的大小时,文本对齐仍然正确)
我可以只通过配置UI来实现,还是需要编码? 这是图像描述

Here is the image description

2 个答案:

答案 0 :(得分:2)

由于图像和文本都是UIButton的一部分并隐藏在界面构建器中,因此实现起来有点棘手。

一些方法:

1)在第二个按钮上添加一个宽度相同的空图像,并保持这两个图像的宽度相同。

2)使用第二个按钮上的标题插图进行播放,将文本移动到正确的位置。标题插入将与图像宽度相关。

3)不要将文本作为按钮的一部分,而是将其设置为UILabel

缺点:您不会在文本中更改按钮(例如,按下时颜色不同)。

优势:可以轻松使用约束,并且您可以获得UILabel的所有好处,例如多行等等。

答案 1 :(得分:1)

我认为它可以帮到你

CGFloat Imagewidth = 10; // width of the image you need to set 
firstButtonBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, Imagewidth);
seconbutton.titleEdgeInsets = UIEdgeInsetsMake(0, Imagewidth, 0, 0);