我正在构建一个通用iOS应用程序,iPad版本使用SplitViewController。在popover视图中,我有一个带有两个按钮的UITabBarController。当它在iPhone上运行时,TabBar按钮会正确拉伸以填充视图的整个宽度......
...但是在iPad上,在弹出视图中,按钮不会拉伸以填充整个宽度......
我正在以编程方式创建UITabBarController ......
InspectionTabBarViewController *inspectionTabBarVC;
InspectionListViewController *inspectionListVC;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
inspectionListVC = [[InspectionListViewController alloc] initWithSunday:NO];
inspectionListVC.managedObjectContext = self.managedObjectContext;
UINavigationController *calendarNavVC = [[UINavigationController alloc] initWithRootViewController:inspectionListVC];
calendarNavVC.title = @"Calendar";
InspectionMapViewController *mapViewVC = [[InspectionMapViewController alloc] initWithNibName:@"InspectionMapView_iPhone" bundle:nil];
UINavigationController *mapdNavVC = [[UINavigationController alloc] initWithRootViewController:mapViewVC];
mapdNavVC.title = @"Map";
inspectionTabBarVC = [[InspectionTabBarViewController alloc] init];
[inspectionTabBarVC addChildViewController:calendarNavVC];
[inspectionTabBarVC addChildViewController:mapdNavVC];
self.window.rootViewController = inspectionTabBarVC;
}
else
{
inspectionListVC = [[InspectionListViewController alloc] initWithSunday:NO];
UINavigationController *calendarNavVC = [[UINavigationController alloc] initWithRootViewController:inspectionListVC];
calendarNavVC.title = @"Calendar";
InspectionMapViewController *mapViewVC = [[InspectionMapViewController alloc] initWithNibName:@"InspectionMapView_iPad" bundle:nil];
UINavigationController *mapdNavVC = [[UINavigationController alloc] initWithRootViewController:mapViewVC];
mapdNavVC.title = @"Map";
inspectionTabBarVC = [[InspectionTabBarViewController alloc] init];
[inspectionTabBarVC addChildViewController:calendarNavVC];
[inspectionTabBarVC addChildViewController:mapdNavVC];
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController_iPad" bundle:nil];
UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
self.splitViewController = [[UISplitViewController alloc] init];
self.splitViewController.delegate = detailViewController;
self.splitViewController.viewControllers = [NSArray arrayWithObjects:inspectionTabBarVC, detailNavigationController, nil];
self.window.rootViewController = self.splitViewController;
inspectionListVC.detailViewController = detailViewController;
inspectionListVC.managedObjectContext = self.managedObjectContext;
detailViewController.detailViewControllerDelegate = inspectionListVC;
}
[self.window makeKeyAndVisible];
我还尝试使用以下语句在InspectionTabBarViewController的loadView方法中设置autoResizeMask ...
self.tabBar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
......但这也不起作用。如何让UITabBar按钮填充整个视图宽度?
非常感谢您的帮助!
答案 0 :(得分:11)
将UITabBar
属性itemPositioning
更改为UITabBarItemPositioningFill
:
self.tabBar.itemPositioning = UITabBarItemPositioningFill;
Swift版本:
tabBar.itemPositioning = .fill
答案 1 :(得分:4)
实际上可以通过在标签栏上设置setSelectionIndicatorImage
来完成。无论iPhone或iPad如何,按钮都会调整为您图像的宽度。
问题在于它不是动态的,每次添加或更改标签栏中的项目数时,您都必须创建另一个图像。
解决方案是在代码中创建图像并按({tab bar width}/{# of tabbaritems}
)计算宽度。
例如:
创建标签栏的位置:
[[self.tabBarController tabBar] setSelectionIndicatorImage:[self selectionIndicatorImage]];
图像方法:
- (UIImage *)selectionIndicatorImage
{
NSUInteger count = [[self.tabBarController viewControllers] count];
CGSize tabBarSize = [[self.tabBarController tabBar] frame].size;
NSUInteger padding = 2;
CGSize buttonSize = CGSizeMake( tabBarSize.width / count, tabBarSize.height );
UIGraphicsBeginImageContext( buttonSize );
CGContextRef c = UIGraphicsGetCurrentContext();
[[UIColor colorWithWhite:0.9 alpha:0.1] setFill];
UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:CGRectMake( padding, padding * 2, buttonSize.width - (padding * 2) , buttonSize.height - ( padding * 2 ) ) cornerRadius:4.0];
[roundedRect fillWithBlendMode: kCGBlendModeNormal alpha:1.0f];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
CGContextRelease( c );
return image;
}
答案 2 :(得分:1)
如果你想创建这种类型的视图,那么使用看起来像标签栏按钮的自定义按钮,并按照你的代码添加功能。
答案 3 :(得分:0)
我在iOS7中找到了itemWidth(UITabBar的属性)。