【发布时间】:2013-02-18 13:25:45
【问题描述】:
在 StackOverflow 上搜索了几个问题后,我发现只有一个名为 BCTabBarController 的用于创建自定义 UITabBar 的主要项目。它的描述是:
使用标准的 UITabBarController 有几个问题 包括:
它太高了,尤其是在横向模式下
高度与 UIToolbar 不匹配
它不能在不使用私有 API 的情况下进行自定义
尽管如此,我发现this strange project on GitHub 和tutorial here 在其实现中使用标准UITabBarController,每个选项卡都使用UIButtons,它正在工作(很奇怪,但确实如此)。
我想知道,如果使用UIButtons 而不是制表符创建自定义UITabBarController 是错误的,它会导致什么结果?其实现如下所示:
- (void)viewDidAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self hideTabBar];
[self addCustomElements];
}
- (void)hideTabBar
{
for(UIView *view in self.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
view.hidden = YES;
break;
}
}
}
-(void)addCustomElements
{
// Initialise our two images
UIImage *btnImage = [UIImage imageNamed:@"NavBar_01.png"];
UIImage *btnImageSelected = [UIImage imageNamed:@"NavBar_01_s.png"];
self.btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button
btn1.frame = CGRectMake(0, 430, 80, 50); // Set the frame (size and position) of the button)
[btn1 setBackgroundImage:btnImage forState:UIControlStateNormal]; // Set the image for the normal state of the button
[btn1 setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; // Set the image for the selected state of the button
btn1.backgroundColor = [UIColor yellowColor];
[btn1 setTag:0]; // Assign the button a "tag" so when our "click" event is called we know which button was pressed.
[btn1 setSelected:true]; // Set this button as selected (we will select the others to false as we only want Tab 1 to be selected initially
在我的项目中,我将使用 iOS 5.1 及更高版本,并且不使用 Storyboard 或 XIB。谢谢!
【问题讨论】:
标签: iphone ios objective-c cocoa-touch uitabbarcontroller