通用iOS应用程序中的自定义选项卡栏
我希望重新创建在许多应用程序中看到的凸起的标签栏,但我希望在一个需要改变凸起位置的通用应用程序中这样做 目前,我没有太多的运气创造这一点,所以我来到这里的启示,如果没有这一点,我可能只是创建一个自定义选项卡栏背景 如果是这样的话,这会在应用程序委托中起作用以区分这两种设备吗通用iOS应用程序中的自定义选项卡栏,ios,uitabbar,Ios,Uitabbar,我希望重新创建在许多应用程序中看到的凸起的标签栏,但我希望在一个需要改变凸起位置的通用应用程序中这样做 目前,我没有太多的运气创造这一点,所以我来到这里的启示,如果没有这一点,我可能只是创建一个自定义选项卡栏背景 如果是这样的话,这会在应用程序委托中起作用以区分这两种设备吗 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { UITarBar *tabBar = [UITabBar appearance]; [tabBar s
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UITarBar *tabBar = [UITabBar appearance];
[tabBar setBackgroundImage: [UIImage imageNamed:@"iPadtabBar.png"]];
}
else {
UITarBar *tabBar = [UITabBar appearance];
[tabBar setBackgroundImage: [UIImage imageNamed:@"iPhonetabBar.png"]];
}
你可能会想,他为什么不直接测试一下。。好吧,那会更容易,如果可以的话,我宁愿先问一下,但我要到下周初才能拿到我的Mac电脑 您必须将uitabarcontroller子类化并自定义该中心选项卡按钮。这里有一个很好的例子: 头文件: @界面CustomTabBarViewController:UITabBarController{ } 实施文件:
@implementation CustomTabBarViewController
// Create a view controller and setup it's tab bar item with a title and image
-(UIViewController*) viewControllerWithTabTitle:(NSString*) title image:(UIImage*)image
{
UIViewController* viewController = [[UIViewController alloc] init];
viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:image tag:0];
return viewController;
}
// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
if (heightDifference < 0)
button.center = self.tabBar.center;
else
{
CGPoint center = self.tabBar.center;
center.y = center.y - heightDifference/2.0;
button.center = center;
}
[self.view addSubview:button];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
@end
这样行吗?设备或模拟器比这里的任何人都更清楚。但更重要的是,在应用程序委托中这样做几乎肯定是错误的。@nhgrif有时我会看到在应用程序委托中设置了选项卡栏。与在故事板或主视图控制器中进行比较,这可能不是普遍接受的方法,但我以前在UITabBar或UITabBarController子类中看到过这样做……这个答案至少应该包括所提供链接中的示例代码段突出显示。子类化uitabarcontroller的基础不太可能在短期内发生太大的变化。。。然而,链接到网站,尤其是非官方文档网站的链接有消亡的趋势,这使得答案完全无用。我通过故事板和许多CollectionViewController来完成所有这些,我需要在选项卡栏上的故事板中插入一个按钮吗?另外,如何设置显示的图像?我是否必须将所述子类导入每个viewcontroller?对不起,完全是新手!
@implementation CustomTabBarViewController
// Create a view controller and setup it's tab bar item with a title and image
-(UIViewController*) viewControllerWithTabTitle:(NSString*) title image:(UIImage*)image
{
UIViewController* viewController = [[UIViewController alloc] init];
viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:image tag:0];
return viewController;
}
// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
if (heightDifference < 0)
button.center = self.tabBar.center;
else
{
CGPoint center = self.tabBar.center;
center.y = center.y - heightDifference/2.0;
button.center = center;
}
[self.view addSubview:button];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
@end