ios)对象-c。如何自定义导航栏上的按钮

时间:2011-07-16 10:55:11

标签: objective-c ios programming-languages

我正在开发一个带有自定义导航栏的导航基础应用程序。我成功地拥有一个带有图像和按钮的导航栏。但是我不知道如何制作自定义按钮.. :(

这是在导航栏上添加“主”按钮的代码。(在视图控制器中,initWithNibName方法)

if (self) {
    // Custom initialization.

    //set navigation id to I to inform that that page is inforview
    navID = @"I";

            //adds the button on the navigation bar
    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"Main" style:UIBarButtonItemStyleBordered target:self action:@selector(goBack:)];
    self.navigationItem.leftBarButtonItem = button;
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    [button release];
    [navID release];

}

谢谢

我创建了一个自定义按钮并将其应用到UIbarbuttonitem。 没有错误但导航栏上没有显示任何内容:( 这是我的代码 -

    //create a custom button
    UIImage *image = [UIImage imageNamed:@"TESTButton.png"];
    UIButton *myCustomButton = [UIButton buttonWithType:UIButtonTypeCustom];
    myCustomButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );    
    [myCustomButton setImage:image forState:UIControlStateNormal];
    [myCustomButton addTarget:nil action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];


    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myCustomButton];
    self.navigationItem.leftBarButtonItem = button;
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;

任何可以修复我的代码的人? :)

1 个答案:

答案 0 :(得分:1)

最简单的方法是使用:

UIButton *yourCustomButton = ....
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:yourCustomButton];
self.navigationItem.leftBarButtonItem = barButtonItem;
[barButtonItem release];