在使用Swift的iOS中,如何创建始终在整个应用程序顶部的可拖动和浮动UIView

时间:2019-01-09 06:40:22

标签: ios iphone swift uiview uiviewcontroller

  • 我想始终将UIView放在顶部(而不是顶部) 像YouTube播放器这样的整个应用中的ViewController)?
  • 我想将此UIView拖动到屏幕上吗? enter image description here

3 个答案:

答案 0 :(得分:0)

您可以使用以下代码将视图添加到应用程序窗口中

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    // Do any additional setup after loading the view, typically from a nib.
    let newView = UIView()
    newView.frame = CGRect(x:0,y:UIApplication.shared.statusBarFrame.height, width: view.frame.size.width, height: 30)
    newView.backgroundColor = UIColor.red
    let label = UILabel()
    label.frame = newView.bounds
    label.text = "header"
    newView.addSubview(label)
    UIApplication.shared.keyWindow?.addSubview(newView)

    let bounds = self.navigationController!.navigationBar.bounds
    self.navigationController?.navigationBar.frame = CGRect(x: 0, y: newView.frame.size.height + newView.frame.origin.y, width: bounds.width, height: bounds.height  )
}

请检查并让我知道是否可行

答案 1 :(得分:0)

将其设为UIWindow的直接子视图,并为其图层设置很高的zPosition

答案 2 :(得分:0)

UIApplication.shared.keyWindow?.addSubview(UIView)