![](/img/trans.png)
[英]How can I properly handle touch events of a UIButton on top of another 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.