iOS自訂UITabBar

來源:互聯網
上載者:User

push頁面時,可調用hidesBottomBarWhenPushed進行隱藏。

第一步,我們需要一些圖片:


各個選項的表徵圖和tabbar的背景圖片,最後還要一個透明的1x1像素的圖片。

第二步,建立一個工程,在工程內建一個繼承於UITabBarController的類。


第三步,首先寫一個方法,返回一個UINavigationController

-(UINavigationController*) viewControllerWithTitle:(NSString*) title image:(UIImage*)image{    UIViewController* viewController = [[UIViewController alloc] init];    viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:image tag:0];    viewController.title = title;    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:viewController];    return nav;}

然後在viewDidLoad裡面建立TabbarController的viewControllers

 self.viewControllers = [NSArray arrayWithObjects:                            [self viewControllerWithTitle:@"1" image:IMG(@"1")],                            [self viewControllerWithTitle:@"2" image:IMG(@"2")],                            [self viewControllerWithTitle:nil image:nil],                            [self viewControllerWithTitle:@"3" image:IMG(@"3")],                            [self viewControllerWithTitle:@"4" image:IMG(@"4")], nil];

看到沒有,比較猥瑣的就是第三個ViewController什麼都沒設定。

因為我們要在那個位置放一個自己的按鈕,繼續在viewDidLoad寫:

    UIButton* button = [[UIButton alloc]initWithFrame:CGRectMake(0.0, 0.0, 65, 65)];    button.center = CGPointMake(160, 20);    [button setBackgroundImage:IMG(@"add") forState:UIControlStateNormal];    [button addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];    [self.tabBar addSubview:button];

然後設定背景圖片:

    [self.tabBar setBackgroundImage:IMG(@"tabbarbg")];

運行之後是這樣的:



會發現按鈕上面有一條橫線,然後再設定這個陰影運行後就沒有人發現你猥瑣的行徑。

    [self.tabBar setShadowImage:IMG(@"transparent")];
最後(iOS7和iOS6):


下載



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.