Master/Parent View Controller Hierarchy
Child View Controller Hierarchy
每当用户在 subview Controller 中向下滚动时,我想隐藏父 View Controller 中的导航栏和 slider 菜单。
In my child view controller, I have a "scroll view -> view -> text view".
In text view, I have text that comes from API as user scrolls down.
主视图 Controller 中的红色突出显示是容器 View ,如图所示。
每当用户在 subview Controller 中向下滚动时,如何隐藏父 View Controller 中的导航栏和 slider 菜单。
是否可以在 UIScrollView 中嵌入容器 View ?
If you have any confusion please mention in comment I will answer it thanks
最佳答案
对于导航栏,您可以轻松检查 UINavigationController
的“隐藏栏”On Swipe
属性。向上滑动时会自动隐藏导航栏,向下滑动时会自动显示导航栏。
但对于 slider 来说,这取决于您如何实现。
您可以为其设置 SwipeGestureRecognizer
并以编程方式处理隐藏属性。
更新
您可以使用 NSNotification
来通知子级的父级。
为此,您应该以这种方式声明它:
class childViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeDown.direction = UISwipeGestureRecognizerDirection.down
self.view.addGestureRecognizer(swipeDown)
}
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
print("Swiped right")
case UISwipeGestureRecognizerDirection.down:
NotificationCenter.default.post(name: .swipedDown, object: nil)
case UISwipeGestureRecognizerDirection.left:
print("Swiped left")
case UISwipeGestureRecognizerDirection.up:
NotificationCenter.default.post(name: .swipedUp, object: nil)
default:
break
}
}
}
}
class ParentViewController: UIViewController {
override func viewDidLoad(){
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(childScrolledUp(notification:)), name: .swipedUp, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(childScrolledDown(notification:)), name: .swipedDown, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self, name: .swipedUp, object: nil)
NotificationCenter.default.removeObserver(self, name: .swipedDown, object: nil)
}
func childScrolledUp(notification: NSNotification) {
//hide your slider menu
}
func childScrolledDown(notification: NSNotification) {
//show your slider menu
}
}
extension Notification.Name {
static let swipedDown = Notification.Name("swipedDown")
static let swipedUp = Notification.Name("swipedUp")
}
关于ios - 从 subview Controller 中隐藏父 View 的 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50076322/