ios - 如何在 UIWebView 之上覆盖 UIButton(关闭)按钮?

标签 ios swift uiwebview

我一直在寻找并尝试解决我在 iOS 的 UIWebView 中遇到的问题。

我有一个非常小的应用程序,它只加载我的网站,源代码位于 https://github.com/pjuu/iotter。 .

在网站上,当用户点击发布的图片时,它会将图片 URL 加载到 Web View 中。这使他们陷入困境,无法返回网站的其他部分。

我想做的是在右上角显示一个 (x) 按钮(无论使用何种屏幕旋转)并在浏览器中执行后退操作。

我知道这个问题不是代码问题,因为我可以对其中的 Swift 代码部分进行排序。我只是一辈子都想不出如何以编程方式添加按钮并让它显示在 webview 上。

我能找到的唯一教程涉及创建一个工具栏来执行这些类型的操作,我认为在查看图像时会占用很多空间。

问题很简单:

  • 这可能吗?
  • 如果是这样,我将如何着手创建按钮?

我将使用正则表达式检查 request.path 参数,以便仅在它与图像 URL 匹配时显示它。

如果这对 SO 来说不够合适,我们深表歉意。我很乐意将问题移至其他网站或发布。

提前感谢您的帮助。我不是移动开发人员,所以使用 XCode 和 Storyboard的东西对我来说是一个远离 vim 的世界。

最佳答案

引用:

1) CGRectMake: https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGGeometry/#//apple_ref/c/func/CGRectMake

2) UIWebViewDelegate: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWebViewDelegate_Protocol/#//apple_ref/occ/intfm/UIWebViewDelegate/webView:shouldStartLoadWithRequest:navigationType :


解决方案:

1) 创建“X”UIButton:

// Define the UIButton

let button   = UIButton(type: UIButtonType.System) as UIButton
let image = UIImage(named: "name") as UIImage?
button.frame = CGRectMake(self.view.frame.width - 60, 30, 35, 35)
button.setTitle("Test Button", forState: UIControlState.Normal)
button.setImage(image, forState: .Normal)
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

self.view.addSubview(button)

//Hide the UIButton

button.hidden = true

2) 检测何时打开 imageUrl 并使“X”UIButton 出现:

// This function is triggered every time a request is made:

optional func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    let string = request.URL

    if string.rangeOfString(".png") || string.rangeOfString(".jpg")|| string.rangeOfString(".jpeg"){
        // code to make cross appear
        // for example:
        self.button.hidden = false
    }
}

3) 最后,您需要创建在点击“X”UIButton 时关闭页面的方法:

func btnPressed(sender: UIButton!) {
    if(webview.canGoBack) {
        //Go back in webview history
        webview.goBack()
    } else {
        //Pop view controller to preview view controller
        self.navigationController?.popViewControllerAnimated(true)
    }
}

注意:

行内:

let image = UIImage(named: "cross.png") as UIImage?
button.setImage(image, forState: .Normal)

我们将 UIButton 设置为十字形。 您需要将交叉图像添加到您的 XCode 项目并将“cross.png”字符串设置为图像的确切名称:“nameOfImage.fileType”。

关于ios - 如何在 UIWebView 之上覆盖 UIButton(关闭)按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37204402/

相关文章:

objective-c - UIPicker 标签与输出不同

swift - 使用 Enum 和 Struct 进行静态和动态截面建模 - Swift 4

javascript - 在 iOS 电子书阅读器中使用 execCommand 突出显示 HTML 的任何替代方法

ios - UIWebView 上的 Unity3D

ios - 需要用于 Appstore 连接和 Xcode 开发人员帐户连接的 URL

ios - 'init' 是否被禁止作为变量名的*部分*?

ios - SKTexture : Error loading image resource

ios - Xcode 6 中 UILabel 的属性字符串

iphone - 根据 iPhone 尺寸的特定类型自动布局 UIButton 和 UILabel 字体大小[非编程方式]

ios - 为什么在动画 block 中显示 View 时 UIWebView scalespagetofit 不起作用?