如何在UIWebView顶部覆盖UIButton(关闭)按钮? [英] How to overlay a UIButton (close) button on top of a UIWebView?

查看:53
本文介绍了如何在UIWebView顶部覆盖UIButton(关闭)按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been looking and trying to solve an issue I have with a UIWebView for iOS.

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

I have a very small app which just loads my website the source code lives at https://github.com/pjuu/iotter.

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

On the site when a user clicks on a posted image it load the image URL in to the web view. This leaves them stranded and unable to get back to other parts of the site.

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

What I want to do is display an (x) button in the top right hand corner (no matter what screen rotation is in use) and have perform the back operation in the browser.

我知道这个问题不是代码问题,因为我可以将其中的Swift代码部分归类.我一生都无法解决如何以编程方式添加按钮并将其显示在Web视图上的问题.

I know this question isn't a code issue as I'm fine to sort of the Swift code part of it. I just could not for the life of me work out how to add the button programmatically and have it display over the webview.

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

The only tutorials I could find involved creating a tool bar to perform these type of actions which I think would take up to much space when looking at an image.

问题很简单:

  • 这可能吗?
  • 如果是的话,我将如何创建按钮?

我将使用正则表达式检查 request.path 参数,使其仅在与图片网址匹配时显示.

I am going to check the request.path parameter with regex to only show it if it matches an image URL.

很抱歉,不能满足SO的要求.我很乐意将问题转移或发布到另一个网站上.

Apologies if this isn't fitting enough for SO. I am happy to move the question or post on another site.

谢谢您的任何帮助.我不是移动开发人员,所以对我来说,使用XCode和情节提要的故事是一个远离vim的世界.

Thank you for any help in advance. I am not a mobile developer so using XCode and the storyboard stuff is a world away from vim for me.

推荐答案

参考:

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

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 :

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)
    }
}


N.B.:

在行中:

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

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

We are setting the UIButton to be the image of a cross. You need to add a cross image to your XCode project and set the "cross.png" String to the exact name of your image: "nameOfImage.fileType".

这篇关于如何在UIWebView顶部覆盖UIButton(关闭)按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆