一旦用户滚动,如何只在导航栏下显示阴影?

时间:2019-07-01 20:10:29

标签: ios swift uinavigationbar

基本上,一旦用户开始滚动,我想在导航栏下方启用阴影半径。导航栏位于TableView控制器中,当第一次打开视图控制器时,导航控制器应处于其正常状态,但是一旦用户开始滚动,就会出现阴影。

以下是我到目前为止在导航栏下方创建阴影的代码:

    //Adds Shadow below navigation bar
    self.navigationController?.navigationBar.layer.masksToBounds = false
    self.navigationController?.navigationBar.layer.shadowColor = UIColor.lightGray.cgColor
    self.navigationController?.navigationBar.layer.shadowOpacity = 0.8
    self.navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0, height: 2.0)
    self.navigationController?.navigationBar.layer.shadowRadius = 2

仅当用户开始滚动时如何启用它?

2 个答案:

答案 0 :(得分:0)

您需要添加这些行以在函数上显示阴影,然后从以下委托方法调用该函数:

$admission_date = $this->input->post('admission_date'); $admissionDetail = ['admission_date' => empty($admission_date) ? '' : date('Y-m-d', strtotime(implode('-',array_reverse(explode('-',$admission_date)))))];

另外,调用showShadow功能的相反方法可能会有所帮助,这将在以下委托方法中移除阴影:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;

答案 1 :(得分:0)

将此添加到您的View Controller:

override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationController?.navigationBar.layer.masksToBounds = false
    self.navigationController?.navigationBar.layer.shadowColor = UIColor.lightGray.cgColor
    self.navigationController?.navigationBar.layer.shadowOpacity = 0
    self.navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0, height: 2.0)
    self.navigationController?.navigationBar.layer.shadowRadius = 2
}


override func scrollViewDidScroll(_ scrollView: UIScrollView) {

    self.navigationController?.navigationBar.layer.shadowOpacity = 0.8
}

如果要在滚动停止时消除阴影,可以通过以下方法将值重置为正常:

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    self.navigationController?.navigationBar.layer.shadowOpacity = 0

}

当用户拖动TableView而不是滚动时,您可能还想做同样的事情,在这种情况下,请同时添加这两个:

override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {

    self.navigationController?.navigationBar.layer.shadowOpacity = 0.8

}

override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {

    self.navigationController?.navigationBar.layer.shadowOpacity = 0

}