Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make Custom UIButton with image and text?

Tags:

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.

How can I make this view with responsive for all device screens.

My UI image is below:

enter image description here

Thanks in advance.

after using CollectionView it shows like this

enter image description here

like image 822
Osman Goni Nahid Avatar asked Apr 11 '17 10:04

Osman Goni Nahid


People also ask

How do I put the image on the right side of the text in a UIButton?

To make an UIButton's image align to the right side of the text, we force content flipping behavior to the one use it right-to-left language. 1 By adding this semanticContentAttribute = . forceRightToLeft , we force UIKit to mirrored our content which push our image to the right side of the text. Here is the result.

How can I check if UIButton is pressed?

If you want to check If UIButton is Pressed or not, You should handle TouchDown Handler and change button's state to pressed in touchDown hadnler. You can track ToucUpInside method to Change state of button to Not-pressed again.


1 Answers

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);
like image 89
Anuraj Avatar answered Sep 22 '22 10:09

Anuraj