简体   繁体   English

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

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

I have read many articles about the SFSafariViewController and I believe that it offers splendid functionality in iOS apps. 我已经阅读了许多有关SFSafariViewController的文章,并且我相信它在iOS应用程序中提供了出色的功能。 However, when I load my SFSafariViewController, I intentionally hide the navigation bar because I want one custom fixed button in the upper left corner to dismiss the view controller. 但是,当我加载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)
    }
}

As you can see, I alter the frame so that the bar at the top is not visible. 如您所见,我更改了框架,以使顶部的条不可见。 That code runs fine. 该代码运行良好。 But when I try to add a UIButton, it appears briefly and then is covered when I run the app. 但是,当我尝试添加UIButton时,它会短暂显示,然后在运行该应用程序时被覆盖。 It's a simple blog reader app that uses the SFSafariViewController. 这是一个使用SFSafariViewController的简单博客阅读器应用程序。 Maybe Apple doesn't want developers running around messing with this, but any solutions or workarounds to make the button stay visible are greatly appreciated! 也许苹果公司不希望开发人员四处乱搞,但任何使按钮保持可见的解决方案或解决方法都将受到赞赏!

Here's the info about the button: 0x7f950b618db0; 这是有关按钮的信息:0x7f950b618db0; frame = (100 400; 100 50); 框架=(100400; 100 50); tag = 1; 标签= 1; layer = CALayer: 0x60000023ab60 层= CALayer:0x60000023ab60

Why don't you use one of the other WebView classes to get the extra functionalities you desire? 为什么不使用其他WebView类之一来获得所需的额外功能?

5.1.1 (iv) SafariViewContoller must be used to visibly present information to users; 5.1.1(iv)必须使用SafariViewContoller向用户显示信息; the controller may not be hidden or obscured by other views or layers . 控制器不会被其他视图或图层隐藏或遮盖 Additionally, an app may not use SafariViewController to track users without their knowledge and consent. 此外,在未经用户了解和同意的情况下,应用程序可能无法使用SafariViewController来跟踪用户。

It is definitely true that Apple won't want the SFSafariViewController to be obscured. 苹果绝对不希望SFSafariViewController被遮盖。 However, I did figure out that when I present it, in the completion block I can add a button and what was causing trouble was that I had to increase the zPosition of the layer of the button's view like so: 但是,我确实弄清楚了,当我展示它时,我可以在完成模块中添加一个按钮,而造成麻烦的是,我不得不像下面这样增加按钮视图层的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.

相关问题 如何正确处理另一个UIButton之上的UIButton的触摸事件? - How can I properly handle touch events of a UIButton on top of another UIButton? 将UIButton放在UIView的顶部 - Place UIButton on the top of a UIView 如何使UIButton始终保持在加载的子视图控制器的顶部? - How to make UIButton stay always on top of loaded child view controllers? 如何在视图顶部放置SpriteKit AdBanner? - How can I place a SpriteKit AdBanner at the Top of the View? 如何将目标视图放在选项卡栏的顶部? - How can I place my target view on top of the tab bar? 尝试将UILabel放在自定义UIAlertView中的UIButton之上 - Trying to place UILabel on top of UIButton in a custom UIAlertView 我可以在 SFSafariViewController 中从 Safari 获得 cookies 吗? - Can I get cookies from Safari in a SFSafariViewController? 如何将SFSafariViewController设置为UISplitViewController的Detail Controller? - How can SFSafariViewController be setup as the Detail Controller for UISplitViewController? 如何在 SwiftUI 中使用 SFSafariViewController? - How do I use SFSafariViewController with SwiftUI? 如何正确地将图像从UIButton放置到图像中? - How do I correctly place the image from a UIButton into an Image?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM