0

i saw many posts in SO on uibutton in navigation controller and on uibutton not working but my problem is uibutton is appearing in view intead on navigation controller here is my code

showMeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
showMeButton.frame = CGRectMake(0, 50, 100, 40);
[showMeButton setTitle:@"User Avtar" 
              forState:UIControlStateNormal];
[showMeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:showMeButton];
[showMeButton addTarget:self action:@selector(showMeButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

why this line of code not working self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:showMeButton];

please help me out... thanks in advance :):)

iMeMyself
  • 1,649
  • 13
  • 28

2 Answers2

1
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button1 setFrame:CGRectMake(00,15,50,26)];
    [button1 setTitle:@"User Avtar" forState:UIControlStateNormal];
    button1.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    [self.navigationController.navigationBar addSubview:button1];
Mani
  • 1,841
  • 15
  • 29
0

Try this

UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
UINavigationItem *item = [navigationBar.items lastObject]; 

Now set your created barbuttonItem to the item's rightBarButtonItem,

item.rightBarButtonItem = button; 

It's works for me.

Diamond King
  • 676
  • 10
  • 17