标签栏iOS xcode中心的按钮

Posted

技术标签:

【中文标题】标签栏iOS xcode中心的按钮【英文标题】:Button in the center of a tab bar iOS xcode 【发布时间】:2014-11-12 17:53:04 【问题描述】:

我认为我的标题是我正在寻找的。所以,我有一个带有 4 个视图控制器的标签栏。我开始在情节提要中配置我的应用程序,并从那里配置了我的标签栏控制器。我也已经在我的 appdelegate 文件中声明了视图控制器的名称。我的问题如下。我可以在标签栏控制器的中间添加一个按钮吗?我对此进行了很多搜索,但没有找到完整的解决方案。我可以使用故事板将我的视图控制器连接到选项卡栏,还可以使用应用程序委托将该按钮声明到选项卡栏的中心吗?请帮忙。到目前为止,这是我在应用程序委托文件中的代码(其他任何内容都在情节提要中,突出连接到 tabbarcontroller 的视图控制器):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Assign tab bar item with titles
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.selectedIndex = 1;
UITabBar *tabBar = tabBarController.tabBar;


UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:3];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:4];


tabBarItem1.title = @"Tab1";
tabBarItem2.title = @"Tab2";

tabBarItem3.title = @"Tab3";
tabBarItem4.title = @"Tab4";




[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"maps_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"maps.png"]];


// Change the tab bar background
UIImage* tabBarBackground = [UIImage imageNamed:@"TAPBARBGR.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selected.png"]];

// Change the title color of tab bar items
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                   [UIColor whiteColor], UITextAttributeTextColor,
                                                   nil] forState:UIControlStateNormal];
UIColor *titleHighlightedColor = [UIColor colorWithRed:153/255.0 green:192/255.0 blue:48/255.0 alpha:1.0];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                   titleHighlightedColor, UITextAttributeTextColor,
                                                   nil] forState:UIControlStateHighlighted];




return YES;

谢谢。

【问题讨论】:

【参考方案1】:

如果您将 4 个页面连接到标准 UITabbarController,它将布局 4 个按钮,平均分配它们之间的空间。您无法控制它们的放置方式。您可以在故事板或代码中重新排列它们,但有 4 个按钮 - 中间没有按钮。

您可以添加第 5 个按钮并将其重新排列到第 3 个位置,因此该按钮将位于标签栏的中间。或者你可以寻找 3rd 方自定义标签栏实现——在 gitnub 和其他地方有很多。其中一些会有特定的中间页面,其样式与其他页面不同。

【讨论】:

【参考方案2】:

我可以在标签栏控制器的中间添加一个按钮吗?

没有。

假设您的意思是添加一个与其他标签栏项目不同的按钮,那么不,UITabBar 不提供此功能。正如UITabBar documentation 中所述,标签栏中的所有项目都应为标签栏项目,但您可以考虑改为UIToolbar

标签栏中的每个按钮都称为标签栏项,并且是一个实例 UITabBarItem 类的。如果你想给用户一个栏 每个执行一个动作的按钮,使用一个 UIToolbar 对象。

从用户体验的角度 (IMO) 来看,尝试混合使用标签栏和工具栏功能可能不是一个好计划。

【讨论】:

以上是关于标签栏iOS xcode中心的按钮的主要内容,如果未能解决你的问题,请参考以下文章

我们如何创建一个更大的中心 UITabBar 项

需要在“更多”按钮所在的标签栏项目中添加图像

无法在标签栏内隐藏中心图像

中心按钮标签栏

在标签栏 xcode 中创建展示案例视图

xcode 4.2 和 ios 5 中的标签栏背景颜色