jwknz
jwknz

Reputation: 6824

ios - Programmatically coded UIButton not showing on my view

.

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

Upvotes: 9

Views: 17501

Answers (7)

shoe
shoe

Reputation: 1080

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.

Upvotes: 0

Nirav Ghori
Nirav Ghori

Reputation: 69

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];

 });

Upvotes: 0

Jeffery Thomas
Jeffery Thomas

Reputation: 42598

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.

Upvotes: 26

Madhava Reddy
Madhava Reddy

Reputation: 11

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

[UIButton buttonWithType: UIButtonTypeRoundedRect]; 

Hope it will help !

Upvotes: 0

Babuli
Babuli

Reputation: 157

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];

Upvotes: 4

CStreel
CStreel

Reputation: 2642

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:

Upvotes: 0

Mark Leonard
Mark Leonard

Reputation: 2096

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.

Upvotes: 4

Related Questions