Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

iphone - Persistent UIBarButtonItem in UIToolbar?

I've been developing an iPhone app that uses a UIToolbar (in the context of a UINavigationController) to display a small status icon at the bottom of the screen. The toolbar will eventually also have action icons associated with it (think Mail application).

I'm running into an issue where it appears that each UIViewController pushed onto the navigation controller's stack is expected to have its own set of items for the toolbar to display, and as a result, the "status" item fades out and back in for each view transition.

Is there a way to have a single persistent item in the toolbar? I also tried adding the item in the navigation controller's initializer (I subclassed UINavigationController for this approach), but it's still no go.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Instead of using the navigation controller's toolbar, add one directly to the window and resize the navigation controller's view's frame to avoid it. That single global toolbar will then always be visible.

If you're using the Navigation-based Application template, and are using Interface Builder, the following steps should do it:

  1. Open up your app delegate's .h file.
  2. Add an IBOutlet UIToolbar * toolbar; to the app delegate's instance variables.
  3. Switch to the .m file.
  4. Find the line that reads [window addSubview:[navigationController view]]; and add after it:
    CGRect frame = navigationController.view.frame;
    frame.size.height -= toolbar.frame.size.height;
    navigationController.view.frame = frame;
  5. Add code to release toolbar in the -dealloc method.
  6. Open MainWindow.xib.
  7. Open the window.
  8. Drag a toolbar onto the bottom of the window.
  9. Connect the toolbar to the app delegate's toolbar outlet
  10. Now set up the toolbar—add whatever items you need to it, then create whatever outlets and actions you need in the app delegate and connect them.

Since the toolbar is part of the window, not part of the navigation controller, the navigation controller shouldn't touch it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...