0

我已经细分UINavigationController到我的 pop & push 需求,现在我想在UINavigationBar. 我已经读到我可以在 appdelegate 中做到这一点(从 iOS 5 开始),但我想将它保留在UINavigationController子类中以备将来使用。

我试过做这样的事情:

-(void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *rightbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightbutton setFrame:CGRectMake(280, 0, 56, 39)];
    [rightbutton setImage:[UIImage imageNamed:@"top.png"] forState:UIControlStateNormal];
    [rightbutton addTarget:self action:@selector(popMePlease) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarButton=[[UIBarButtonItem alloc] initWithCustomView:rightbutton];
    [[self  navigationItem] setRightBarButtonItem:rightBarButton];
}

但我看不到 rightBarButton。不知道有没有viewDidLoad地方放。

4

3 回答 3

0

试试这个,它工作正常。

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0, 0, 40, 40);
[myButton setBackgroundImage:[UIImage imageNamed:@"imTitleBG.png"] forState:UIControlStateNormal];
UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem = rightBtn;
于 2013-06-11T09:44:53.697 回答
0

在您的代码按钮 x 位置从 280 开始。它应该接近于 0。

替换以下行

[rightbutton setFrame:CGRectMake(280, 0, 56, 39)];

[rightbutton setFrame:CGRectMake(0, 0, 56, 39)];
于 2013-06-11T07:59:53.510 回答
-1

试试这个代码

 UIBarButtonItem *btn = [[UIBarButtonItem alloc]
                       initWithCustomView:myButton];
        self.navigationItem.rightBarButtonItem=btn;

-(void)Action
{

// Do what you want here 

}

试试这个代码可能对你有帮助......

于 2013-06-11T08:11:10.240 回答