简体   繁体   English

如何使用图像和文本制作自定义UIButton?

[英]How can I make Custom UIButton with image and text?

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 . 正如我在Android上在布局下安排了一个ImageView和TextView并在Layout上设置ClickListener所以同样的事情我怎么能在swift (3.0) Xcode 8 There is no markup like XML. 没有像XML这样的标记。 It's difficult to do with drag and drop. 拖放很难做到。

How can I make this view with responsive for all device screens. 如何通过响应所有设备屏幕来创建此视图。

My UI image is below: 我的UI图片如下:

在此输入图像描述

Thanks in advance. 提前致谢。

after using CollectionView it shows like this 使用CollectionView后,它显示如下

在此输入图像描述

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);

您可以在UIButton中设置UIImage + UIText,以供参考https://developer.apple.com/documentation/uikit/uibutton

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

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