xcode 如何使用图像和文本制作自定义 UIButton?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43343206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make Custom UIButton with image and text?
提问by Osman Goni Nahid
As I did on Android under a layout arrange an ImageView and TextView and set ClickListener on Layout so same thing how can I do on swift (3.0) Xcode 8
.
There is no markup like XML. It's difficult to do with drag and drop.
正如我在布局下的 Android 上所做的那样,安排 ImageView 和 TextView 并在 Layout 上设置 ClickListener 所以同样的事情我该怎么做swift (3.0) Xcode 8
。没有像 XML 那样的标记。拖放很难做到。
How can I make this view with responsive for all device screens.
如何使此视图响应所有设备屏幕。
My UI image is below:
我的用户界面图片如下:
Thanks in advance.
提前致谢。
after using CollectionView it shows like this
使用 CollectionView 后,它显示如下
回答by Anuraj
You can use this code to set image and title to button
您可以使用此代码将图像和标题设置为按钮
let imageSize: CGSize = button.imageView!.image!.size
button.titleEdgeInsets = UIEdgeInsetsMake(26 , -imageSize.width, 0.0, 0.0);
let labelString = NSString(string: button.titleLabel!.text!)
let titleSize = labelString.sizeWithAttributes([NSFontAttributeName: button.titleLabel!.font])
button.imageEdgeInsets = UIEdgeInsetsMake(-15, 0.0, 0.0, -titleSize.width);
回答by shubh14896
You can set UIImage + UIText inside UIButton , for reference https://developer.apple.com/documentation/uikit/uibutton
您可以在 UIButton 中设置 UIImage + UIText ,以供参考 https://developer.apple.com/documentation/uikit/uibutton