问题描述
在这篇文章中继续我的问题,我想发布一个完整的问题,这将是很多 xamarin.ios 开发人员的问题.
In continue of my question in this post, I want to post a complete question which will be a question lots of xamarin.ios developers.
我的要求是在所有 UIViewController 中都有 TabBar.所以,据我所知,有两种方法可以实现它.
My request is having TabBar in ALL UIViewControllers. So, as I know, there are two ways to realize it.
第一:
appDelegate -> 设置 RootViewController : TabController -> UVC1
在这种情况下,我有 NULL NavigationController,我将没有 navigationItem.并在
in this case, I have NULL NavigationController and I'll have no navigationItem. and in
this.NavigationController.PushViewController(newSearchViewController(), true);
NavigationController 为空会报错.
It makes error that NavigationController is null.
这是我在 AppDelegate 中的代码:
Here is my code in AppDelegate:
_tabController = new TabController();_window.RootViewController = _tabController;
和我的 TabController :
and my TabController :
公共类 TabController : UITabBarController{
public class TabController : UITabBarController {
UIViewController tab1, tab2, tab3, tab4;
public TabController()
{
tab1 = new HomeViewController();
tab1.TabBarItem.Image = UIImage.FromFile("Icons/Home.png");
tab2 = new TagCategoryViewController(null, null, 1, null);
tab2.TabBarItem.Image = UIImage.FromFile("Icons/Tag.png");
tab3 = new SearchViewController();
tab3.TabBarItem.Image = UIImage.FromFile("Icons/Search.png");
tab4 = new ProfileViewController();
tab4.TabBarItem.Image = UIImage.FromFile("Icons/Home.png");
var tabs = new UIViewController[] {
tab1, tab2, tab3,tab4
};
ViewControllers = tabs;
}
}
第二种方式:
RootViewController -> navigationController -> TabController -> UVC1 ->新的 UVC2 -> 没有标签栏!!
在这里,一切听起来都不错,但是当我导航到 Tabs 中不存在的新 UIViewController 时,TabBar 将消失!
Here, everything sounds good, but when I navigate to new UIViewController which is not present in Tabs, the TabBar will diappear !
代码是:
_tabController = new TabController();
var navigationController = new UINavigationController(viewController);
_window.RootViewController = new UINavigationController(_tabController);
我能做什么?任何的想法?
What can I do ? Any idea?
我不使用故事板!
推荐答案
通过用 UINavigationController
包装所有的 UIViewController
你可以启用你想要的行为,但要确保您删除 TabBarController
,因为 NavigationBar
将与视图中的 NavigationBar
重叠.
By wrapping all your UIViewController
with UINavigationController
you can enable the behaviour you want, but make sure that you remove TabBarController
since the NavigationBar
will overlap the NavigationBar
from your Views.
_window.RootViewController = _tabController;
以及您的观点:
tab1 = new UINavigationController(new HomeViewController());
tab1.TabBarItem.Image = UIImage.FromFile("Icons/Home.png");
这篇关于xamarin.ios 中的 UITabBarController 不使用 StoryBoard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!