2017-03-09 48 views
0

我在UITabBarViewController添加定製中心按鈕,如下面的代碼沒有隱瞞......的UITabBarController自定義按鈕,同時移動到下一個視圖控制器

self.centerButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
self.centerButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin; 
self.centerButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height); 
[self.centerButton setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
[self.centerButton setBackgroundImage:highlightImage forState:UIControlStateHighlighted]; 

CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height; 
    if (heightDifference < 0) { 
     self.centerButton.center = self.tabBar.center; 
    } else { 
     CGPoint center = self.tabBar.center; 
     center.y = center.y - heightDifference/2.0; 
     self.centerButton.center = center; 
    } 

[self.centerButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; 

[self.view addSubview:self.centerButton]; 

請檢查連接參考截圖,

enter image description here

現在,當我試圖隱藏它而移動到其他viewcontroller時,只有tabbar被隱藏,而不是與下面的代碼中心自定義按鈕,

[self.tabBarController.tabBar setHidden:YES]; 

也試過像這樣隱藏中心的自定義按鈕,

WBTabBarController *objWBTab = [self.storyboard instantiateViewControllerWithIdentifier:ID_CNTRL_TABBAR]; 
[objWBTab setTabBarHidden:YES]; 

SetTabBarHidden已WBTabBarController.m被定義爲以下,

- (void)setTabBarHidden:(BOOL)tabBarHidden 
{ 
    self.centerButton.hidden = tabBarHidden; 
    self.tabBar.hidden = tabBarHidden; 
} 

仍然沒有運氣,任何人可以幫助我嗎?

+0

你爲什麼不只是添加'UIButton'作爲標籤欄的子視圖而不是'self.view'? – Rikh

+0

由於自定義按鈕的高度較大,所以我必須添加它在self.view,我不能將它添加到標籤欄 – Gati

+0

嗯不設置剪輯界限爲false的tabbar做同樣的事情?像按鈕可以在標籤欄的邊界外可見 – Rikh

回答

1

它,因爲你」將你的按鈕作爲子視圖添加到整個視圖而不是tabBar的視圖。

變化[self.view addSubview:self.centerButton];符合
[self.tabBar addSubview:self.centerButton];

+0

自定義按鈕不會添加到我的bottomBar中。它被添加到tabbarviewcontroller的self.view中。因此,如果我要隱藏底部欄,仍然不會隱藏自定義按鈕。由於自定義按鈕的高度較大,所以我必須添加它在self.view,我不能將它添加到tabbar – Gati

0

嘗試推送檢查隱藏底酒吧的ViewController要在故事板enter image description here

打開,或者您也可以使用它,因爲這在編程接近

 self.hidesBottomBarWhenPushed = true 
     let destinationVC = self.storyboard?.instantiateViewControllerWithIdentifier("StoryBoardID") as! yourVC 
     self.navigationController?.pushViewController(destinationVC, animated: true) 
     self.hidesBottomBarWhenPushed = false 
0

讓試圖掩蓋它,當視圖將消失

-(void) viewWillDisappear:(BOOL)animated{ 
    [self setTabBarHidden:YES]; 
    [super viewWillDisappear:animated]; 
} 

,並顯示視圖時會出現

-(void) viewWillAppear:(BOOL)animated{ 
    [self setTabBarHidden:NO]; 
    [super viewWillAppear]; 
} 
+0

viewWillDisappear沒有得到調用,而從tabbarcontroller移動到其他viewcontroller::( – Gati

相關問題