繁体   English   中英

如何在已加载的SFSafariViewController顶部正确放置UIButton?

[英]How can I properly place a UIButton on top of a loaded SFSafariViewController?

我已经阅读了许多有关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的简单博客阅读器应用程序。 也许苹果公司不希望开发人员四处乱搞,但任何使按钮保持可见的解决方案或解决方法都将受到赞赏!

这是有关按钮的信息:0x7f950b618db0; 框架=(100400; 100 50); 标签= 1; 层= CALayer:0x60000023ab60

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

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

苹果绝对不希望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)
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM