ios - Programmatically coded UIButton not showing on my view
Asked Answered
B

7

9

.

Hello,

I am trying to add a UIButton and other items to my UIViewController programmatically and from what I have read and seen in other SO questions I should be on the right track with this code:

- (void)viewDidLoad
{
[super viewDidLoad];



UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];    
backButton = [[UIButton alloc] init];
backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(10, 25, 150, 75);
backButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
backButton.titleLabel.text = @"Back"; 
[self.view addSubview:backButton];


// Do any additional setup after loading the view.
}

Now that code is placed in the viewDidLoad Method and it does not appear, which leads me to the question - What am I doing wrong?

When I want to change the color of the background and put in the viewDidLoad it works fine.

Cheers Jeff

Battue answered 25/5, 2012 at 0:47 Comment(6)
can u submit your viewDidLoad codeEsterify
I have updated the code to show the full method - I am just practicing so it is pretty empty:-)Battue
As a side note you are creating three instances of the button. Two are unnecessary.Darren
Yup I see that now thanks for that:-)Battue
Have you tried setting the background color of the button to something other than the background color of the main view and seeing if the button shows up? Maybe it is on the screen, you just can't see it for some reason--you don't have an image set for it, after all. (Stranger things have happened.)Vorfeld
Yeah that worked thanks:-) If you don't mind posting it as an answer, I can accept it:-)Battue
D
26

I think you may be inserting the button correctly, but just can't see it because it has no background and the text isn't showing.

Try using a rounded rect button system button, just to see if that makes it show up. Set the text correctly. I'll also remove setting the font, just incase there's a problem with the font.

// backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
backButton = [UIButton buttonWithType:UIButtonTypeSystem];
backButton.frame = CGRectMake(10, 25, 150, 75);
[backButton setTitle:@"Back" forState:UIControlStateNormal];
[self.view addSubview:backButton];

Update: UIButtonTypeRoundedRect has beed deprecated.

Darren answered 25/5, 2012 at 0:59 Comment(1)
Agreed UIButtonTypeRoundedRect provided the information necessary to allow the title to show. However since it's now deprecated we will need to use UIButtonTypeCustom, or System (if nav related) instead, as mentioned here: developer.apple.com/library/ios/documentation/UIKit/Reference/…Bottoms
P
4

You are setting the button title incorrectly. Use:

- (void)setTitle:(NSString *)title forState:(UIControlState)state

Also as others have said you don't need to initialize your button three times. Remove the 2nd and 3rd lines.

Protolithic answered 25/5, 2012 at 0:56 Comment(0)
H
4

You have created button using static method buttonWithType ,Again you have allocated memory for that button.

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(10, 25, 150, 75);
backButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
backButton.titleLabel.text = @"Back";
[self.view addSubview:backButton];
Highhat answered 4/1, 2013 at 5:35 Comment(0)
E
0

Aside from the issues pointed out in the comments

UIButtonTypeCustom, creates a blank button.

Use this when you want to use an Image as a button, but keep in mind using setImage: overrides the title of the button. So you will need to set a BackgroundImage.

Your button is actually there it just doesn't "appear" to be there

So

Use UIButtonTypeRoundRect (it might be slightly different)

or if you want an image as a button.

Use UIButtonTypeCustom Call setBackgroundImage: then call setTitle:forState:

Esterify answered 25/5, 2012 at 0:58 Comment(0)
A
0

You have to use button type is RoundedRect then we can see the background color

[UIButton buttonWithType: UIButtonTypeRoundedRect]; 

Hope it will help !

Antiphonal answered 15/5, 2015 at 10:38 Comment(0)
R
0

You can use dispatch_after to call a block later. so here first view are loaded it will take within 0.1 sec and then all button are visible In view. Xcode, start typing dispatch_after and hit Enter to autocomplete to the following:

Try this:

 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

  UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];    
  backButton = [[UIButton alloc] init];
  backButton = [UIButton buttonWithType:UIButtonTypeCustom];
  backButton.frame = CGRectMake(10, 25, 150, 75);
  backButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
  backButton.titleLabel.text = @"Back"; 
  [self.view addSubview:backButton];

 });
Riptide answered 5/6, 2015 at 13:10 Comment(2)
Hey @Nirav - Thanks for the code, but perhaps add an explanation of what its purpose is in relation to the answer. Makes you more poplular :-) Also put all your code inside the code block.Battue
He's showing how to setup a thread to be called 0.1 secs after the view is loaded and at that time add the programmatically added button. I found this while I was in some code which did not work when at full speed, but would run if I single stepped the code.Reniti
N
0

For me the issue was that the combination of both the default color of the UIButton's text and background with the background color of my app made it appear as if the button wasn't there when it was; just very difficult to see. Setting the text color to something that contrasted with the app's background fixed the issue.

Nonlinearity answered 28/6, 2021 at 22:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.