問題描述
我正在使用 Tito 的代碼片段將自定義按鈕添加到我的標(biāo)簽欄:https://github.com/tciuro/CustomTabBar
I am using the code snippet from Tito to add a custom button to my tab bar: https://github.com/tciuro/CustomTabBar
(繼承 UITabbarController 并使用添加自定義按鈕
(Subclassing UITabbarController and adding a custom button using
// .. created a UIButton *button
[self.view addSubview:button];
)
這對我的基于故事板的應(yīng)用程序非常有用,但導(dǎo)航控制器中的子視圖啟用了在推送時(shí)隱藏底欄"選項(xiàng)的情況除外.這會按承諾隱藏標(biāo)簽欄,但不會隱藏自定義按鈕.似乎按鈕應(yīng)該作為子視圖添加到標(biāo)簽欄本身?我嘗試了這個(gè)丑陋的代碼,它甚至沒有顯示按鈕:
This works great with my storyboard-based app except for the case of a subview within a navigation controller with the option "Hides bottom bar on push" enabled. This hides the tab bar as promised, but not the custom button. Seems like the button should be added as a subview to the tab bar itself? I tried this ugly code which did not even make the button show up:
for(UIView *view in self.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view addSubview:button];
break;
}
}
有什么想法嗎?
更新:我的解決方案:在我的 ApplicationDelegate 中,我定義了以下方法,只要需要,我就會在 viewWillAppear 或 viewWillDisappear 方法中調(diào)用它們:
UPDATE: My solution: In my ApplicationDelegate i define the following methods, which i call whenever needed in the viewWillAppear or viewWillDisappear methods:
-(void)hideCenterButton:(BOOL)animated
{
if(animated){
[UIView animateWithDuration:0.3
delay:0.0f
options:UIViewAnimationCurveLinear
animations:^{
CGRect frame = self.centerButton.frame;
frame.origin.x = -100;
self.centerButton.frame = frame;
}
completion:^(BOOL finished){
}];
}
}
-(void)showCenterButton:(BOOL)animated
{
if(animated){
[UIView animateWithDuration:0.35
delay:0.0f
options:UIViewAnimationCurveLinear
animations:^{
CGRect frame = self.centerButton.frame;
frame.origin.x = (self.view.superview.frame.size.width / 2) - (self.centerButton.frame.size.width / 2);
self.centerButton.frame = frame;
}
completion:^(BOOL finished){
}];
}
}
我必須將動(dòng)畫的持續(xù)時(shí)間設(shè)置為 0.35 秒才能獲得與標(biāo)簽欄相協(xié)調(diào)的平滑效果.
I had to set the animation's duration to 0.35s to get a smooth effect in harmony with the tab bar.
推薦答案
我認(rèn)為有兩種方法可以解決這個(gè)問題.
I think there are 2 ways you can got with this.
1) 嘗試將按鈕放入舊頂視圖控制器上方的視圖和標(biāo)簽欄但在被推送的新頂視圖控制器下方的視圖中.
1) try to get the button into a view that is above the old top view controller and the tab bar BUT below the new top view controller that is pushed.
2) 當(dāng)新的視圖控制器被按下時(shí),按鈕動(dòng)畫消失.
2) animate away the button when the new view controller is pushed.
第一個(gè)需要處理未記錄、不受支持且隨時(shí)可能更改的 iOS 專有視圖層次結(jié)構(gòu).
The first will require mucking with the iOS proprietary view hierarchy which is undocumented, unsupported and could change anytime.
第二個(gè)問題是讓動(dòng)畫看起來足夠流暢,讓您的用戶不會注意到.這不完全是表現(xiàn)完美的問題,只是表現(xiàn)得恰到好處.
The second will be a matter of making the animation appear smooth enough for your user not to notice. It's not entirely a matter of behaving perfect, just appearing appropriately.
我個(gè)人會推薦按鈕消失的動(dòng)畫(將其 alpha 設(shè)置為 0)并根據(jù)您的視圖控制器越過標(biāo)簽欄是出現(xiàn)還是消失而重新出現(xiàn).
I would personally recommend an animation of the the button disappearing (animate it's alpha to 0) and reappearing based on if your view controller that goes over the tab bar is appearing or disappearing.
導(dǎo)航動(dòng)畫是(我相信)0.3 秒.如果按鈕位于標(biāo)簽欄的中間,您可能希望它在視圖控制器中的動(dòng)畫到達(dá)它時(shí)不可見(如果不是更早的話),因此可以使用 0.1 到 0.15 秒之間的時(shí)間將其動(dòng)畫出來.
The animation for a navigation is (I believe) 0.3 seconds. If the button is in the middle of the tab bar, you'll likely want it invisible as the animating in view controller reaches it (if not sooner) so something between 0.1 and 0.15 seconds could be used to animate it out.
現(xiàn)在這并沒有使按鈕的行為與標(biāo)簽欄完全相同,但是由于轉(zhuǎn)換的速度如此之短,用戶不會真正注意到它.
Now this does not make the button behave exactly the same as the tab bar, but with the quickness of the transition being so short, it will be unnoticeable really to the user.
現(xiàn)在只是提供一個(gè)問題讓您問自己.為什么需要推送一個(gè)與標(biāo)簽欄重疊的視圖控制器?為什么這比呈現(xiàn)模態(tài)視圖控制器更可取/必要?如果您可以強(qiáng)烈支持它,請堅(jiān)持下去,祝您好運(yùn),如果沒有必要,您也許可以使用模態(tài)視圖控制器獲得您想要的體驗(yàn).
Now just to provide a question for you to ask yourself. Why do you need to push a view controller that overlaps the tab bar? Why is that more desirable/necessary than presenting a modal view controller? If you can strongly argue for it, keep at it and good luck, if it's not necessary however, you may be able to achieve the experience you want with a modal view controller.
這篇關(guān)于當(dāng) hidesBottomBarWhenPushed 為“TRUE"時(shí)如何隱藏自定義標(biāo)簽欄按鈕的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!