- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我创建了一个 UITabBarController
,其中包含多个 UIViewControllers
作为其选项卡。我还想在此 UITabBarController
中显示一个常量 UINavigationBar
,它将跨选项卡持续存在。由于 UITabBarController
的 subview Controller 都不是 UINavigationControllers
,我希望这是可能的并且不会引起冲突。有办法做到这一点吗?
目前我正在将完全相同的 UINavigationBar
和相应的逻辑复制粘贴到每个 subview Controller ,所以我猜有更好的方法。
关于这为何有用:想象一个 UINavigationBar
,它包含两个 UIBarButtonItems
,用于“注销”和“帐户”。按“注销”将使您注销并推送一个模态视图 Controller ,以便您可以重新登录,而按“帐户”将调出一个带有您的帐户信息的模态视图 Controller 。
我在 Stack Overflow 上发现了这个现有问题,但问题本身及其评论都措辞不当且信息不足:How to add a navigation bar to UITabBarController?
更新:
我觉得我可能没有很好地解释自己,所以我创建了一个图形来说明我正在尝试做的事情。在下图中,我有一个 UITabBarController
,它以四个 UIViewController
作为子级呈现。我希望有一个不变的 UINavigationBar
,以便用户可以按 Done
关闭模态呈现的 View Controller 。由于每个 subview Controller 都是一个单独的 View ,因此似乎没有必要将每个 subview Controller 嵌入到它自己的 UINavigationController
中,以便让 UINavigationBar
出现。当前,当 UITabBarController
以模态方式呈现时,不会出现导航栏。
最佳答案
我终于让它工作了!!
简而言之:您不能继承或保留 UINavigationBar
,但您可以通过其 UINavigationItem 设置其
。UIBarButtonItem
这是我所做的,然后是相关代码,然后是注意事项:
1) UITabBarController 子类:我创建了自己的 UITabBarController
自定义子类。因为我希望我的两个 UIBarButtonItem
跨选项卡持续存在,所以我还在我的 UITabBarController
子类中创建了 @property
。因为我还希望我的 UITabBarController
处理这些 UIBarButtonItem
,所以我还在我的 UITabBarController
子类中为每个按钮创建了一个方法。
2) 在 UINavigationControllers 中为选项卡嵌入 View Controller :即使您不想要或不需要 UINavigationController
,这是获得预期效果的最佳方式,因为在iOS 7 导航栏在状态栏下方向上延伸到屏幕顶部。 (据我所知,我无法在没有 UINavigationController
的情况下使用 UINavigationBar
在 iOS 7 中复制这种效果。)
3) Assign UIBarButtonItems To Top View Controllers:我将 UITabBarController
子类中的 UIBarButtonItem
分配给了我想要的 View Controller 标签栏 Controller 的 -[initWithNibName:bundle:]
和 -[awakeFromNib]
方法。我通过 self.viewControllers
遍历在我的选项卡栏 Controller 中分配为选项卡的所有 View Controller 来完成此操作;检查它们是否是 UINavigationController
;并通过 [navigationController.topViewController.navigationItem setLeftBarButtonItem:self.leftBarButton]
和 [navigationController. topViewController.navigationItem setRightBarButtonItem:self.RightBarButton]
.
// MyTabBarController.h //
@interface MyTabBarController () <UITabBarControllerDelegate>
@property (nonatomic, strong) UIBarButtonItem *leftBarButton;
@property (nonatomic, strong) UIBarButtonItem *rightBarButton;
- (void)setup;
- (void)buttonLeft:(UIBarButtonItem *)sender;
- (void)buttonRight:(UIBarButtonItem *)sender;
@end
@implementation MyTabBarController
@synthesize leftBarButton = _leftBarButton;
@synthesize rightBarButton = _rightBarButton;
- (UIBarButtonItem *)leftBarButton
{
if (!_leftBarButton) _leftBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Left" style:UIBarButtonItemStyleBordered target:self action:@selector(buttonLeft:)];
return _leftBarButton;
}
- (UIBarButtonItem *)rightBarButton
{
if (!_rightBarButton) _rightBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Right" style:UIBarButtonItemStyleBordered target:self action:@selector(buttonRight:)];
return _rightBarButton;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
[self setup];
}
return self;
}
- (void)awakeFromNib
{
[self setup];
}
- (void)setup
{
[self setDelegate:self]; // since I am using myself as the UITabBarControllerDelegate
for (UIViewController *viewController in self.viewControllers)
{
if ([viewController isKindOfClass:[UINavigationController class]])
{
UINavigationController *navigationController = (UINavigationController *)viewController;
[navigationController.topViewController.navigationItem setLeftBarButtonItem:self.leftBarButton];
[navigationController.topViewController.navigationItem setRightBarButtonItem:self.rightBarButton];
}
}
}
- (void)buttonLeft:(UIBarButtonItem *)sender
{
// Left bar button action
}
- (void)buttonRight:(UIBarButtonItem *)sender
{
// Right bar button action
}
@end
在实现我的解决方案的过程中,我学到了很多东西,包括以下内容:
UINavigationBar
子类并将其设置为您的 UINavigationController
如所述通过 Interface Builder/Storyboard here (external website: iOS Dev Notes) .关于objective-c - 跨 subview Controller 从 UITabBarController 继承/保留 UINavigationBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21447080/
我正在开发一个广播流应用程序,应用程序底部有 2 个 UITabBarController 按钮。一种用于直播,一种用于前 40 名列表。 我为这两个选项卡使用了一个通用的 StreamingView
我有一个 UINavigationController在我的 UITabBarController 的根目录中.我还有一个 UITabBarController我现在在现有的 UITabBarCont
我最近升级到 xcode 4。我的应用程序使用 tabbarcontroller。在旧的 xcode 3.x 中,您可以根据此屏幕截图将选项卡更改为不同类型: 根据我在研究此问题时收集到的信息,xco
我使用包含 3 个项目的 Storyboard创建了一个 UITabBarController,前两个是 View Controller 。我需要第三项指向另一个 UITabBarController
好的,这就是场景: 我有一个选项卡栏应用程序,每个选项卡中都有一个 UINavigationController。假设我有两个选项卡:“主页”和“退出”。在“主页”中,用户遵循基于 UINavigat
我有自己的 UITabBarViewController 子类。是否可以更改所有嵌入式 View Controller View 的框架,以便自己的 UITabBarViewController Vi
滚动时选择顶部的菜单项。我希望用户突出显示一个项目,然后单击它以选择它。 -> 当用户移动到其他菜单项时,选定的蓝色会随之移动(跟随光标)。如果用户单击该项目,则选定的蓝色将被新项目替换,如果不是在用
我正在尝试以编程方式更改我的应用程序中标签栏项目的名称。 在我的 Storyboard 我有一个 UITabBarController 设置为初始 View ,层次结构如下: UITabBarCont
我正在使用包含 3 个项目的 UITabBarController,并且很好奇如果我位于第二个或第三个选项卡上,如何从第一个选项卡访问方法。我遇到的问题是我在第一个选项卡上有一个 UIImageVie
我希望在 iOS 中实现一个自定义选项卡栏,其中所选项目的尺寸大于其余选项卡的大小,并查看类似于此屏幕截图的内容。 有人可以指出如何最好在 Swift 中完成此任务的教程吗? 最佳答案 我多次面临这个
我正在制作一个自定义的东西,它的工作方式应该与 UITabBarController 类似——一个管理子 ViewController 的 UIViewController。父 UIViewContr
我有一个 viewController,它正在作为 UITabBarController 上的选项卡加载。 我如何知道在 viewController 类中,有多少区域可用于显示此 viewContr
我想在UITabbarController中输入一个按钮来处理事件作为instagram应用程序的标签栏。 此链接中的图片: http://itunes.apple.com/us/app/instag
我有一个 UIViewController,其中包含一个带有三个选项卡的 UITabBarController(每个选项卡都是一个 UINavigationController)。 我遇到的问题是,在
我在使用 UITabBarController 时遇到了真正的问题。我追求的结果如下:1)在纵向模式下,一个简单的基于选项卡栏的应用程序(带有导航栏)没什么特别的。2)在横向模式下,我想使用自己的 U
UITabBarController 不允许横向。所以我使用了 UITabBarContoller 的子类(称为 RotatingTabBarController)。其唯一目的是通过向 shouldA
是否可以用动画隐藏它? 最佳答案 UITabBar 继承自 UIView,因此您可以像使用标准 UIView 一样隐藏它并为其设置动画。 - (void) hideTheTabBarWithAnima
UITabBarController是否可以在代码中选择选项卡?就像UINavigationController一样我们可以使用popViewController弹回rootViewControlle
我需要更改 UITabBarController 的“更多”按钮显示的列表的字体大小和背景颜色。是否可以 ?我该怎么做 ?非常感谢。 最佳答案 UITabBarController 有一个名为 mor
我正在尝试将股票UITabBar的高度更改为44px,类似于Tweetbot的标签栏高度。我也看到其他一些应用程序也可以这样做。 但是,当我尝试设置高度时,它仍然保持不变 self.tabBar.fr
我是一名优秀的程序员,十分优秀!