ios - 在使用 swift 的 iOS 中,如何创建一个始终位于整个应用程序顶部的可拖动且 float 的 UIView

标签 ios iphone swift uiview uiviewcontroller

  • 我想让 UIView 始终位于顶部(而不是顶部) ViewController)像 youtube 播放器一样贯穿整个应用???
  • 我想让这个 UIView 可拖动到屏幕上? enter image description here

最佳答案

您可以使用以下代码将 View 添加到您的应用程序窗口

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  )
}

请检查并告诉我这是否有效

关于ios - 在使用 swift 的 iOS 中,如何创建一个始终位于整个应用程序顶部的可拖动且 float 的 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54104470/

相关文章:

swift - 什么减少(_ :_:) function do in Swift?

arrays - 根据键的值创建多维数组

ios - FBSDKShareLinkContent 属性的 "deprecated from Graph API 2.9"警告

ios - 保存和读取图像阵列

ios - leftbarbuttonitem 不显示在导航栏中

iphone - 如何使用比较:options对NSArray进行排序

ios - TouchID activateTouchWithResponse 返回成功而不请求指纹

ios - openfire 聊天服务器在发送表情符号时出错

ios - 如何在ios中获取paypal(1.4.3)的交易ID?

ios - 带有自定义单元格的 UIlabel 不出现