問題描述
我正在使用 Tito 的代碼片段將自定義按鈕添加到我的標簽欄: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];
)
這對我的基于故事板的應用程序非常有用,但導航控制器中的子視圖啟用了在推送時隱藏底欄"選項的情況除外.這會按承諾隱藏標簽欄,但不會隱藏自定義按鈕.似乎按鈕應該作為子視圖添加到標簽欄本身?我嘗試了這個丑陋的代碼,它甚至沒有顯示按鈕:
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 方法中調用它們:
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){
}];
}
}
我必須將動畫的持續時間設置為 0.35 秒才能獲得與標簽欄相協調的平滑效果.
I had to set the animation's duration to 0.35s to get a smooth effect in harmony with the tab bar.
推薦答案
我認為有兩種方法可以解決這個問題.
I think there are 2 ways you can got with this.
1) 嘗試將按鈕放入舊頂視圖控制器上方的視圖和標簽欄但在被推送的新頂視圖控制器下方的視圖中.
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) 當新的視圖控制器被按下時,按鈕動畫消失.
2) animate away the button when the new view controller is pushed.
第一個需要處理未記錄、不受支持且隨時可能更改的 iOS 專有視圖層次結構.
The first will require mucking with the iOS proprietary view hierarchy which is undocumented, unsupported and could change anytime.
第二個問題是讓動畫看起來足夠流暢,讓您的用戶不會注意到.這不完全是表現完美的問題,只是表現得恰到好處.
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.
我個人會推薦按鈕消失的動畫(將其 alpha 設置為 0)并根據您的視圖控制器越過標簽欄是出現還是消失而重新出現.
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.
導航動畫是(我相信)0.3 秒.如果按鈕位于標簽欄的中間,您可能希望它在視圖控制器中的動畫到達它時不可見(如果不是更早的話),因此可以使用 0.1 到 0.15 秒之間的時間將其動畫出來.
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.
現在這并沒有使按鈕的行為與標簽欄完全相同,但是由于轉換的速度如此之短,用戶不會真正注意到它.
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.
現在只是提供一個問題讓您問自己.為什么需要推送一個與標簽欄重疊的視圖控制器?為什么這比呈現模態視圖控制器更可取/必要?如果您可以強烈支持它,請堅持下去,祝您好運,如果沒有必要,您也許可以使用模態視圖控制器獲得您想要的體驗.
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.
這篇關于當 hidesBottomBarWhenPushed 為“TRUE"時如何隱藏自定義標簽欄按鈕的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!