【问题标题】:Add UIButton in a UIWebView在 UIWebView 中添加 UIButton
【发布时间】:2011-06-14 15:51:10
【问题描述】:

我有一个 UIWebView 已被我的请求 (loadRequest) 填充。

我想在其中添加一个 UIButton。编写这个简单的代码很容易:

[self.myWebView loadRequest:request];
[self.myWebView addSubview:myButton];

但是,UIButton 似乎不会随着 UIWebView 的内容滚动。它像 UIWebView 顶部的层一样保持固定。因此,当用户滚动时,UIButton 会与内容不同步。

有什么想法吗?

【问题讨论】:

  • 尝试在界面构建器中将按钮添加到 web 视图,或者您可以减小 web 视图的框架大小并将按钮添加到视图。

标签: iphone objective-c ios


【解决方案1】:

您可以注入 html 标记,例如 使用 stringByEvaluatingJavaScriptFromString 方法进入 UIWebview 的内容。

要捕获点击事件并防止它重新加载您的 webview 的内容,请使用此委托:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if(navigationType==UIWebViewNavigationTypeLinkClicked && [[request.URL absoluteString] isEqualToString: @"yourTag01"])
    {
        //your custom action code goes here
        return NO;
    }
    return YES;
}

【讨论】:

  • 似乎是一个有趣的解决方案。我明天会试试这个,让你知道,谢谢;)
  • 成功了,谢谢 :) 我使用这种方法的问题是重新加载 UIWebView(使用 js-frame)。
  • 你能不能,简要解释一下你在这里展示的概念,。谢谢你
  • 如果你的按钮有一个 onclick 处理程序会更好,它在一个不可见的 iframe 中加载一个虚拟 url (yourTag01),因为如果你快速按下按钮多次,它可能不会每次都被认可
【解决方案2】:

你不能以任何理智直接的方式做到这一点。您可能需要重新评估您正在做的事情。

【讨论】:

    【解决方案3】:

    继续 axiixc 的评论,这里有一些代码可以用来在 webview 的底部放置一个按钮。通过将定位代码放在布局子视图中,您可以正确处理旋转。

    - (void)webViewDidFinishLoad:(UIWebView *)webview{  
        if (!_button){
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            [button setTitle:@"Move All Blue Cards to Known" forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"signin-button-blue-color"] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont boldSystemFontOfSize:16.0];
            [_webview.scrollView addSubview:button];
            _button = button;
        }
    
        [self setNeedsLayout];
        [self layoutIfNeeded];
    
    }
    
    
    - (void)layoutSubviews{
        [super layoutSubviews];
    
        float y = 0;
        CGRect originalRect = _webview.frame;
        _webview.frame = CGRectMake(0, 0, originalRect.size.width, 1); // Trick the webview into giving the right size for content
        CGSize contentSize = _webview.scrollView.contentSize;
        _webview.frame = originalRect;
        if (contentSize.height < _webview.frame.size.height){ // This keeps the button at the bottom of the webview, or at the bottom of the content, as needed.
            y = _webview.frame.size.height - 64; // 44 tall + 20 offset from the bottom
        } else {
            y = contentSize.height + 20;
        }
    
        _button.frame = CGRectMake(20, y, self.frame.size.width-40, 44); // 40/2 = 20 px on each side
        contentSize.height = CGRectGetMaxY(_button.frame)+20;
        _webview.scrollView.contentSize = contentSize;    
    }
    

    【讨论】:

      【解决方案4】:

      在 iOS 5 上,您可以直接访问 UIWebView 的滚动视图,这是您真正想要添加按钮的位置,https://developer.apple.com/documentation/uikit/uiwebview/1617955-scrollview。但我倾向于同意之前的评论,您可能应该考虑一种不同的方法,因为将 UIKit 与 Web 视图混合并不能真正按照您的描述工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-17
        • 1970-01-01
        • 1970-01-01
        • 2023-03-31
        • 2016-01-07
        相关资源
        最近更新 更多