ios - 从 subview Controller 中隐藏父 View 的 UIView

标签 ios swift xcode uiscrollview

How my screen looks like

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 属性。向上滑动时会自动隐藏导航栏,向下滑动时会自动显示导航栏。

enter image description here

但对于 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/

相关文章:

ios - Firebase 获取重复数据

ios - 在 tableviewheader 中使用 UITextField 进行 self.view.endEditing 时崩溃

xcode - 如何在 Swift 中的文件之间共享常量和变量

ios - 在 swiftui 中关闭 View

xcode - 连接文件所有者以查看问题

ios - 如何调整 UIButton 的 imageSize?

ios - 如何让我的应用程序图标进入 iOS 8 锁屏的左下角?

ios - 使用企业分发在 iOS 8 和 iOS 9 上推送通知

javascript - jQuery 选择菜单更改功能在 iPhone 上不起作用

ios - XC测试 : Can not get the last collection cell element after swipeLeft()