i want to add a button having both image and text, text below the image. can anyone guide me.I am adding button programmatically in UIScrollview. Thanks
Asked
Active
Viewed 3,444 times
0
-
6Possible duplicate of [Programmatically set image and text on UIButton](http://stackoverflow.com/questions/9887959/programmatically-set-image-and-text-on-uibutton) – Ricardo Alves Mar 06 '17 at 10:06
2 Answers
4
The code below represents one way to create a UIButton
programmatically in Objective C.
UIButton *mButton = [UIButton buttonWithType:UIButtonTypeCustom];
[mButton addTarget:self action:@selector(clickedMe:) forControlEvents:UIControlEventTouchUpInside];
[mButton setBackgroundImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];
[mButton setTitle:@"Show View" forState:UIControlStateNormal];
mButton.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[yourScrollView addSubview:mButton];

cit
- 2,465
- 5
- 28
- 36

Fahim Parkar
- 30,974
- 45
- 160
- 276
1
If you are using Interface Builder, there is a very easy way to do this:
- Select the button and set a title and an image. Note that if you set the background instead of the image then the image will be resized if it is smaller than the button.
- Set the position of both items by changing the edge and insets. You could even control the alignment of both in the Control section.
Example :
OR
You could even use the same approach by code, without creating UILabels and UIImages inside as other solutions proposed.
OR
[btn setBackgroundImage:buttonImage forState:UIControlStateNormal];

Sid Mhatre
- 3,272
- 1
- 19
- 38