如何在加载的SFSafariViewController上正确放置UIButton?

时间:2017-08-17 02:32:54

标签: ios swift xcode uibutton

我已经阅读了很多关于SFSafariViewController的文章,我相信它在iOS应用程序中提供了出色的功能。但是,当我加载我的SFSafariViewController时,我故意隐藏导航栏,因为我想在左上角有一个自定义固定按钮来关闭视图控制器。

    override func viewDidAppear(_ animated: Bool) {
    let safariViewController = PSSafariViewController(url: URL(string: blogUrl)!, entersReaderIfAvailable: true)
    present(safariViewController, animated: false) {
        var frame = safariViewController.view.frame
        let OffsetY: CGFloat  = 44
        frame.origin = CGPoint(x: frame.origin.x, y: frame.origin.y - OffsetY)
        frame.size = CGSize(width: frame.width, height: frame.height + OffsetY)
        safariViewController.view.frame = frame
        let btn: UIButton = UIButton(frame: CGRect(x: 100, y: 400, width: 100, height: 50))
        btn.backgroundColor = UIColor.green
        btn.setTitle("Click Me", for: .normal)
        btn.addTarget(self, action: #selector(PSBlogViewController.buttonAction), for: UIControlEvents.touchUpInside)
        btn.tag = 1 // change tag property
        btn.isOpaque = true
        safariViewController.view.addSubview(btn)
        safariViewController.view.bringSubview(toFront: btn)
        print(btn.description)
    }
}

如您所见,我改变了框架,使顶部的条形图不可见。该代码运行正常。但是当我尝试添加UIButton时,它会短暂显示,然后在我运行应用程序时会被覆盖。这是一个使用SFSafariViewController的简单博客阅读器应用程序。也许Apple不希望开发人员乱用这个,但任何使按钮保持可见的解决方案或解决方法都非常感谢!

这是关于按钮的信息:0x7f950b618db0; frame =(100 400; 100 50); tag = 1; layer = CALayer:0x60000023ab60

2 个答案:

答案 0 :(得分:0)

为什么不使用其他WebView类来获得所需的额外功能?

  

5.1.1(iv)必须使用SafariViewContoller向用户显示信息;控制器可能不会被隐藏或遮挡   其他视图或图层。此外,应用程序可能不会使用   SafariViewController在他们不知情的情况下跟踪用户   同意。

答案 1 :(得分:0)

苹果绝对不希望SFSafariViewController被遮挡。但是,我确实弄清楚当我呈现它时,在完成块中我可以添加一个按钮,导致问题的是我不得不像这样增加按钮视图层的zPosition

    present(safariViewController, animated: false) {
                var frame = safariViewController.view.frame
                let OffsetY: CGFloat  = 44
                frame.origin = CGPoint(x: frame.origin.x, y: frame.origin.y - OffsetY)
                frame.size = CGSize(width: frame.width, height: frame.height + OffsetY)
                safariViewController.view.frame = frame
                self.btn = UIButton(frame: CGRect(x: 5, y: 50, width: 50, height: 50))
                self.btn.layer.cornerRadius = 25
                self.btn.backgroundColor = UIColor(red: 0, green: 170/255, blue: 240/255, alpha: 0.5)
                self.btn.setTitle("←", for: .normal)
                self.btn.addTarget(self, action: #selector(safariViewController.buttonAction(sender:)), for: .touchUpInside)
                self.btn.tag = 1 // change tag property
                self.btn.isOpaque = true
                safariViewController.view.addSubview(self.btn)
                safariViewController.view.bringSubview(toFront: self.btn)
                self.btn.layer.zPosition = safariViewController.view.layer.zPosition + 1
                for subview in safariViewController.view.subviews {
                    subview.isUserInteractionEnabled = false
                }
                self.btn.isEnabled = true
                self.btn.isUserInteractionEnabled = true
                //print(self.btn.description)
            }