在 Swift 中将 UIButton 覆盖在 UIWebView 上

2023-07-06移动开发问题
1

本文介绍了在 Swift 中将 UIButton 覆盖在 UIWebView 上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

所以我有一个加载 UIWebView 的视图,我想覆盖 UIButton 用户可以点击以转到另一个视图.

So I have a view that loads a UIWebView and I want to overlay UIButton the user can tap on to go to another view.

我设法将按钮放在网络视图上,但我似乎无法点击它.

I managed to place the button over the web view but I can't seem to be able to tap on it.

这是我的代码:

class ButtonView: UIView {
    var button: UIButton!

    override init(frame: CGRect) {
        super.init(frame: frame)

        button = UIButton(frame: CGRectMake(0, 0, 50, 50))
        button.layer.zPosition = 10
        self.addSubview(button)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("Init(coder:) has not been implemented")
    }    
}

class MyViewController : UIViewController, UIWebViewDelegate {

    var buttonView = ButtonView()
    @IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.loadWebView() //This just loads a url to the web view...

        webView.layer.zPosition = 1
        buttonView = ButtonView(frame: CGRectMake(0, 0, 100, 100))
        buttonView.layer.zPosition = 100
        buttonView.button.addTarget(self, action: "buttonEvent:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(buttonView)
    }

    func buttonEvent(sender: AnyObject!){
        println("Tap detected!")
    }
}

所以这会按预期在 web 视图顶部显示按钮,但我无法点击它.

So this displays the button on top of the web view as expected but I can't tap on it.

虽然如果我隐藏网页视图,我可以毫无问题地点击按钮

Although if I hide the web view I can tap on the button without any problems

这是 zPosition 问题吗?还是别的什么?

Is this a zPosition problem? or something else?

推荐答案

只要你更换它就可以工作:

It could work as it is as long as you replace:

self.view.addSubview(buttonView)

与:

webView.addSubview(buttonView)

所以基本上将包含按钮的视图添加到 web 视图中

So basically add the view containing the button to the web view instead

这篇关于在 Swift 中将 UIButton 覆盖在 UIWebView 上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

[ios.cocos2d+box2d]如何禁用自动旋转?
[ios.cocos2d+box2d]how to disable auto-rotation?([ios.cocos2d+box2d]如何禁用自动旋转?)...
2024-08-12 移动开发问题
7

从 Documents 目录存储和读取文件 iOS 5
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)...
2024-08-12 移动开发问题
9

如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?
How can I implement a virtual joystick for a cocos2d game outside the cocos2d environment?(如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?)...
2024-08-12 移动开发问题
13