问题
In my tvOS app I have a TabBarController with 3 viewControllers. What I want to do is to automatically hide/change focus of the tabBar when I switch to the next viewController.
I saw some posts here, on SO that suggested to change alfa on the tabBar, but I would like to have a slide up animation, same way as it does when you change focus to something in the viewController.
Any kind of help is highly appreciated.
回答1:
As Charles said.. Something like this in the derived UITabBarController:
var focusOnChildVC : Bool = false {
didSet {
self.setNeedsFocusUpdate()
}
};
override weak var preferredFocusedView: UIView? {
get {
let v : UIView?;
let focused = UIScreen.mainScreen().focusedView
//A bit of a hack but seems to work for picking up whether the VC is active or not
if (focusOnChildVC && focused != nil) {
v = self.selectedViewController?.preferredFocusedView
} else {
//If we are focused on the main VC and then clear out of property as we're done with overriding the focus now
if (focusOnChildVC) {
focusOnChildVC = false
}
v = super.preferredFocusedView;
}
return v
}
}
回答2:
The basic idea of the solution described below is to subclass UITabBarController
and selectively use the super
implementation of weak var preferredFocusedView: UIView? { get }
or one that returns selectedViewController?.preferredFocusView
along with an implementation of didUpdateFocusInContext(_:withAnimationCoordinator:)
that sets up an NSTimer
that triggers a focus update and sets a flag that controls the preferredFocusView
implementation.
More verbosely, Subclass UITabBarController
and override didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator)
. In your implementation (make sure to call the super
implementation) you can inspect the context and determine if a descendent view of the tabBar
property is the nextFocusedView
or the previousFocusedView
(and the nextFocusedView
is not a descendent).
If the tab bar is gaining focus you can create an NSTimer
for the duration that you want to show the tab bar before hiding it. If the tab bar loses focus before the timer fires, invalidate it. If the timer fires, call setNeedsFocusUpdate()
followed by updateFocusIfNeeded()
.
The last piece you need to get this to work is a flag that is set to true
while the timer is set. You then need to override weak var preferredFocusedView: UIView? { get }
and call the super
implementation if the flag is false
and if it is true
return selectedViewController?.preferredFocusedView
.
回答3:
You can do it in a UITabBarController subclass:
final class TabBarViewController: UITabBarController {
private(set) var isTabBarHidden = false
func setTabBarHidden(_ isHidden: Bool, animated: Bool) {
guard isTabBarHidden != isHidden else {
return
}
var frame: CGRect
let alpha: CGFloat
if isHidden {
frame = tabBar.frame
frame.origin.y -= frame.height
alpha = 0
} else {
frame = tabBar.frame
frame.origin.y += frame.height
alpha = 1
}
let animations = {
self.tabBar.frame = frame
self.tabBar.alpha = alpha
}
if animated {
UIView.animate(withDuration: 0.3, animations: animations)
} else {
animations()
}
isTabBarHidden = isHidden
}
}
来源:https://stackoverflow.com/questions/35447148/how-to-slide-tabbar-up-to-hide-it-in-tvos-app