ios - 向 UIWindow 添加一个按钮。最佳实践?

标签 ios xcode swift swift2

所以我一直在努力在屏幕上添加一个按钮,它根本不会移动(即使使用 ScrollView /表格 View )。

所以,基本上是一个像 Android 中一样的 float 操作按钮。

我使用了以下库,它们工作正常,但有错误并且不是最佳的:

  • KCFloatingActionButton
  • 液体 float 操作按钮

所以我决定创建自己的按钮。 现在的问题是我不知道在哪里添加按钮。我正在使用标签栏 Controller

我看过这个question .

它提出了以下解决方案:

1)使用UIViewController而不是UITableVC,但我不可能这样做。

2)由于添加了自动布局,第二种解决方案不起作用。

3)另一种方法是在导航 Controller 上添加按钮(如果我有导航 Controller )。

我个人正在考虑将其添加到 UIWindow 上。但我不知道我是否应该这样做。添加 float 操作按钮的建议和最佳实践是什么?

这是我尝试过的:

    let fab : UIButton = UIButton()


    fab.translatesAutoresizingMaskIntoConstraints = false
    fab.center.x = (self.navigationController?.view.center.x)! + (self.navigationController?.view.frame.width)!/2 - 50.0
    fab.center.y = (self.navigationController?.view.center.y)! + (self.navigationController?.view.frame.height)!/2 - 110.0
    fab.frame.size = CGSize(width: 60.0, height: 60.0)


    self.navigationController?.view.addSubview(fab)

    var constraintSet = [NSLayoutConstraint]()

    constraintSet.append(fab.centerXAnchor.constraintEqualToAnchor(self.navigationController?.view.centerXAnchor, constant: self.view.frame.width/2 - 50.0))
    constraintSet.append(fab.centerYAnchor.constraintEqualToAnchor(self.navigationController?.view.centerYAnchor, constant: self.view.frame.height/2 - 110.0))
    constraintSet.append(fab.heightAnchor.constraintEqualToConstant(60.0))
    constraintSet.append(fab.widthAnchor.constraintEqualToConstant(60.0))
    self.navigationController?.view.addConstraints(constraintSet)

    fab.layer.zPosition = 3.0
    fab.setImage(UIImage(named: "fab"), forState: .Normal)

这是很棒的图片:如果有人想尝试

enter image description here

最佳答案

您可以使用 keyWindow 并将按钮添加为 subview 。

 var window :UIWindow = UIApplication.sharedApplication().keyWindow
 window.addSubview(yourButton)

关于ios - 向 UIWindow 添加一个按钮。最佳实践?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37876006/

相关文章:

ios - 如何将项目导入 Xcode

ios - UISplitView,设置委托(delegate)时接收语义问题

swift - Xcode 6 修复了 TableView Controller 中的 View

function - Xcode (Swift) 使用另一个函数中的变量

ios - 在 Swift 中访问自定义 UITableViewCell UILabel 文本会引发错误

ios - 多个 UIViews,传递方法

iphone - 稍后从UIImagePickerController压缩视频吗?

ios - 从 App 打开 map 返回 Nil 作为自定义点注释的位置标题

ios - 通话进行时打开应用程序时相机卡住

objective-c - UIPopoverController 委托(delegate)问题?