uibarbuttonitem没有显示uibutton

时间:2012-06-15 07:40:42

标签: uinavigationcontroller uibutton uibarbuttonitem uinavigationitem

我在导航控制器中的uibutton上看到很多帖子,而uibutton上没有工作,但我的问题是uibutton出现在导航控制器上的intead这里是我的代码

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

为什么这行代码不起作用self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:showMeButton];

请帮帮我...提前谢谢:):))

2 个答案:

答案 0 :(得分: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];

答案 1 :(得分:0)

试试这个

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

现在将您创建的barbuttonItem设置为项目'rightBarButtonItem,

item.rightBarButtonItem = button; 

它对我有用。