【发布时间】:2012-07-01 02:45:02
【问题描述】:
我希望有一个带有设置按钮的导航栏,无论它在哪个选项卡上,它都保持在那里。
抱歉,短信来晚了,
追逐
【问题讨论】:
标签: objective-c uitabbarcontroller uinavigationbar
我希望有一个带有设置按钮的导航栏,无论它在哪个选项卡上,它都保持在那里。
抱歉,短信来晚了,
追逐
【问题讨论】:
标签: objective-c uitabbarcontroller uinavigationbar
您可以将 UIToolBar 拖到情节提要控制器上,然后在其中设置所需的按钮和操作。你可以让你的所有控制器都从同一个基类扩展,这样就不必重复你的代码,如果你觉得这个工具栏更令人愉悦,也可以在代码中实例化这个工具栏。
问题在于使用导航时,导航栏将替换/重叠此工具栏。
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:@"Title"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(myAction:)];
[myToolbar setItems:[NSArray arrayWithObjects:myButton, nil]];
[self.view addSubview:myToolbar];
【讨论】:
在 AppDidFinishLaunching 中:
Add Your TabBarcontroller (rootViewController) for UINavigationController
现在在下面添加设置按钮是导航栏中的示例
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonSystemItemDone target:nil action:nil];
navigationCOntroller.navigationItem.rightBarButtonItem = rightButton
rightButton 将通过应用程序可见
【讨论】:
如果您只想放置一个带有设置按钮的导航栏,可以使用 UITabBarController 从任何视图访问,您可以在 AppDelegate 中执行类似的操作 -->
YourViewController1 *yourVC1 = [YourViewController1 alloc] initWithNibName:@"YourViewController1" bundle:nil];
UINavigationController *yourNVC1 = [[UINavigationController alloc] initWithRootViewController:yourVC1];
YourViewController2 *yourVC2 = [YourViewController2 alloc] initWithNibName:@"YourViewController2" bundle:nil];
UINavigationController *yourNVC2 = [[UINavigationController alloc] initWithRootViewController:yourVC2];
UITabBarController *tabBC = [[UITabBarController alloc] init];
[tabBC setViewControllers:[NSArray arrayWithObjects:yourNVC1, yourNVC2, nil]];
self.window.rootViewController = tabBC;
并且您应该在所有视图上都有一个带有导航栏的标签栏。现在要将设置按钮放置在导航栏中,将其添加到视图控制器中要显示设置按钮的 viewDidLoad 方法中 -->
UIBarButtonItem *selectBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Settings" style:UIBarButtonItemStyleBordered target:self action:@selector(selectorName:)];
self.navigationItem.rightBarButtonItem = selectBarButton;
【讨论】: