简体   繁体   English

使UIButton大于其图像

[英]Make UIButton bigger than its image

I have created a number of UIButton in my iOS application using the interface builder. 我已经使用界面构建器在iOS应用程序中创建了许多UIButton Each UIButton also has its own image (no title). 每个UIButton也都有自己的图像(无标题)。

I would like to increase the size of the button without disturbing the image size inside of it (making the touch-area bigger without changing the image size). 我想增加按钮的大小,而又不影响按钮内部的图像大小(在不改变图像大小的情况下增大触摸区域)。

Is this possible to do using the interface builder? 使用界面生成器可以做到吗? I have played around with pretty much every option in the button page but have not find anything yet? 我在按钮页面上几乎使用了每个选项,但是还没有找到任何东西吗?

found the answer finally here 终于找到答案

In Xcode 8 the button content/title/image insets have moved to the Size Inspector tab but we all know that and it's still not working, but there is one more step in the alignment in the below image 在Xcode 8中,按钮的content/title/image插入已移至“ 大小检查器”选项卡,但我们都知道并且它仍然无法正常工作,但是下图中的对齐方式还需要进行一步

说明

May be you want to increase the button's focus area, but the button's image still not be changed. 可能是您想增加按钮的焦点区域,但是按钮的图像仍然没有改变。 I have two ways. 我有两种方法。

1. the image you can use UIImageView to addSubView // To set imageView's position with freedom. 1.可以使用UIImageView的图像添加SubView // To set imageView's position with freedom. imageView // To set imageView's position with freedom. Then override the function: 然后覆盖函数:

在此处输入图片说明

2. After set button's frame, then set the image's position and size .eg: [button setImageEdgeInsets:UIEdgeInsetsMake(up, left,bottom, right)]; 2.在设置按钮的框架之后,然后设置图像的位置和大小。例如:[button setImageEdgeInsets:UIEdgeInsetsMake(up,left,bottom,right)]; 在此处输入图片说明

You need to set button's height and width as per require (if autolayout enable) . 您需要根据需要设置按钮的高度和宽度(如果启用了自动布局)。 it should be larger than image size . 它应该大于图像大小。

And now set button's contentEdges : 现在设置按钮的contentEdges:

button.contentEdgeInsets = UIEdgeInsetsMake(15, 20, 10, 10);  //Change x,y,width,height as per your requirement.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM