0

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

S_J
  • 35
  • 1
  • 9
  • 6
    Possible 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 Answers2

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:

  1. 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.IB basic image and title
  2. Set the position of both items by changing the edge and insets. You could even control the alignment of both in the Control section.

IB position set IB Control set

Example : Button preview

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