将UIView粘贴到屏幕顶部

时间:2015-09-30 13:03:39

标签: ios swift uiview uiscrollview

我尝试将UIView粘贴到屏幕顶部。

我使用以下代码:

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

var navigationBarOriginalOffset : CGFloat?

@IBOutlet weak var navigationBar: UIView!
@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()
    scrollView.delegate = self
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    navigationBarOriginalOffset = navigationBar.frame.origin.y
}

func scrollViewDidScroll(scrollView: UIScrollView) {
    navigationBar.frame.origin.y = max(navigationBarOriginalOffset!, scrollView.contentOffset.y)
    print("did scroll")
}
}

但视图并不坚持,它与滚动视图一起滚动。

我怎样才能坚持下去?

1 个答案:

答案 0 :(得分:1)

您可以添加要粘贴到主视图或顶部布局指南的视图的约束。这可以解决问题