gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:47:29 27 4
gpt4 key购买 nike

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

这是在导航栏上添加“主”按钮的代码。(在 View Controller 中,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;

谁能修复我的代码? :)

最佳答案

最简单的方法是使用:

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

关于objective-c - ios)对象-c。如何自定义导航栏上的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6716878/

27 4 0