How to create a custom UIButton with image and text?

As on Android under the layout, position the ImageView and TextView and install the ClickListener in the layout in the same way I can do on swift (3.0) Xcode 8. There is no markup like XML. This is hard to do with drag and drop.

How can I make this view based on all device screens.

Image of my interface below:

enter image description here

Thanks in advance.

after using CollectionView it looks like

enter image description here

+4
source share
2 answers

You can use this code to set the image and name on the 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);
+1
source

Source: https://habr.com/ru/post/1674563/


All Articles