erdemgc
erdemgc

Reputation: 1721

Buttons are not seen on the view

I have added buttons programmatically to my view, but only toolbar is visible on the view when i test on the simulator.

What am i doing wrong?

I have defined my ui elements in header file, and synthesized in a main file and then initialized in the viewDidLoad method. But there is something wrong.

My code is like below;

#import "ViewController.h"

@implementation ViewController


@synthesize iAmLost, iAmLooking, about, howToUse,appsFromKodAtolye, exit;


- (void)viewDidLoad
{
[super viewDidLoad];

toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, 320.0, 40.0)];

iAmLost = [[UIButton alloc]initWithFrame:CGRectMake(0,0, 200.0, 10.0)]; 

iAmLooking = [[UIButton alloc ]initWithFrame:CGRectMake(0, 0, 200.0, 10.0) ];

[iAmLooking addTarget:self 
             action:@selector(iAmLookingScreen) 
   forControlEvents:UIControlEventTouchUpInside];


about = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200.0, 10.0)];

howToUse = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200.0, 10.0)];

exit = [[UIBarButtonItem alloc] initWithTitle:@"Exit" style:UIBarButtonItemStyleBordered  target:self action:@selector(exitApp:)];

NSArray *buttons = [[NSArray alloc] 
                    initWithObjects:exit, nil];

toolbar.items = buttons;


[self.view addSubview:iAmLost];
[self.view addSubview:iAmLooking];
[self.view addSubview:about];
[self.view addSubview:toolbar];



}

Upvotes: 2

Views: 143

Answers (4)

Preetham
Preetham

Reputation: 125

Simple, just check the origin x & y for each Button

UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, 320.0, 40.0)];

    UIButton *iAmLost = [[UIButton alloc]initWithFrame:CGRectMake(5,50, 200.0, 10.0)];
    iAmLost.backgroundColor = [UIColor redColor];
    UIButton * iAmLooking = [[UIButton alloc ]initWithFrame:CGRectMake(0, 70, 200.0, 10.0) ];

    [iAmLooking addTarget:self
                   action:@selector(iAmLookingScreen)
         forControlEvents:UIControlEventTouchUpInside];


    UIButton * about = [[UIButton alloc]initWithFrame:CGRectMake(0, 90, 200.0, 10.0)];
    about.backgroundColor = [UIColor greenColor];

   UIButton *  howToUse = [[UIButton alloc]initWithFrame:CGRectMake(0, 120, 200.0, 10.0)];
    howToUse.backgroundColor = [UIColor blackColor];

  UIBarButtonItem*  exit = [[UIBarButtonItem alloc] initWithTitle:@"Exit" style:UIBarButtonItemStyleBordered  target:self action:@selector(exitApp:)];

    NSArray *buttons = [[NSArray alloc]
                        initWithObjects:exit, nil];

    toolbar.items = buttons;


    [self.view addSubview:iAmLost];
    [self.view addSubview:iAmLooking];
    [self.view addSubview:howToUse];
    [self.view addSubview:about];
    [self.view addSubview:toolbar];

Upvotes: 0

Anand Gautam
Anand Gautam

Reputation: 2579

You have total four buttons and one tool bar. The frame of tool bar is totally correct ie

       (0, 0, 320.0, 40.0) 

But for buttons you are giving same coordinates for all, so it is overlapping. Better you will change button coordinates. And if you want to add these buttons on tool bar then set coordinate properly and use

     [toolBar addSubView: button];

Upvotes: 1

Adam Richardson
Adam Richardson

Reputation: 2526

You are placing the buttons underneath the toolbar, you add each item at 0,0. As the toolbar is the last item you add to the subview it will appear on top of all the buttons and therefore you will not be able to see the buttons.

The frame is made up of 4 parts x co-ordinate, y co-ordinate, height, width

Try changing the x and y co-ordinates for these

For example

toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, 320.0, 40.0)];

iAmLost = [[UIButton alloc]initWithFrame:CGRectMake(0,100, 200.0, 10.0)]; 

iAmLooking = [[UIButton alloc ]initWithFrame:CGRectMake(0, 200, 200.0, 10.0) ];

You should also note that you currently have not set any style for the buttons or any titles. It might be worth setting titles or a background colour so that you can actually see what buttons you have placed on screen and tell which is which.

Upvotes: 3

Mat
Mat

Reputation: 7633

All your buttons are below the toolbar since you're adding the toolbar as the last subview. You have to add your buttons as subviews of the toolbar. Also be aware that you're positioning all the buttons at the same position.

Upvotes: 2

Related Questions