开发者

How to make leftBarButtonItem looking like backBarButtonItem?

开发者 https://www.devze.com 2023-04-04 05:49 出处:网络
The default solvings are not appropriate: to change the previous ViewController title - I need to make my own function controlling the button touches up

The default solvings are not appropriate:

  1. to change the previous ViewController title - I need to make my own function controlling the button touches up
  2. to make a leftBarButtonItem and hide backBarButtonItem - leftBarButtonItem doesn't look like a def开发者_Python百科ault backBarButtonItem.


Here's an example to create a custom leftBarButtonItem:

UIImage *buttonImage = [UIImage imageNamed:@"backbutton.png"];
UIButton *aButton = [UIbutton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0,0.0,buttonImage.size.width,buttonImage.size.height);
[aButton addTarget:self action:@selector(aSelector) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:aButton];
self.navigationItem.leftButtonItem = backButton;

Hope it helps...

Edit:

Try this...

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"yourimage.jpg"] style:UIBarButtonItemStyleBordered target:self action:@selector(aSelector)];
self.navigationItem.leftBarButtonItem = backButton;

I don't remember what type of button is the backbutton, try to change the default style to other. Good luck!


Just create a custom barbuttonitem and customize it with a picture similar to the back button item. You can get its text from the view controllers in the navigation stack.

Then handle the taps to implement the desired effect


You can assign your custom views as the bar buttons to your navigation controller, like this:

UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:yourCustomizedControl];
[yourCustomizedControl release];

self.navigationItem.leftBarButtonItem = customItem;
[customItem release];

Check this Apple documentation for a more detailed explanation, if you want.


Here is a hack to add a back-like button:

UINavigationItem* backNavigationItem = [[UINavigationItem alloc] initWithTitle:@"Back"];
[_navigationBar pushNavigationItem:backNavigationItem animated:NO];
[backNavigationItem release];

You can add more items (such like title, right button) this way:

UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"Title"];
navigationItem.rightBarButtonItem = // Some UIBarButtonItem
[_navigationBar pushNavigationItem:navigationItem animated:NO];
[navigationItem release];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号