Pierre Espenan
Pierre Espenan

Reputation: 4066

Add UIButton in a UIWebView

I have a UIWebView which is already filled by my request (loadRequest).

I would like to add a UIButton in it. It's easy to write this simple code :

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

However, it seems the UIButton won't scroll with the UIWebView's content. It stays fixed like a layer on top of the UIWebView. So when the user scrolls, the UIButton gets un-synchronized with the content.

Any ideas ?

Upvotes: 3

Views: 6436

Answers (4)

axiixc
axiixc

Reputation: 1972

On iOS 5 you can directly access a UIWebView's scroll view, which is where you really want to add your button, https://developer.apple.com/documentation/uikit/uiwebview/1617955-scrollview. But I tend to agree with the previous commenters, you should probably be considering a different approach as mixing UIKit with a web view isn't really meant to work how you describe.

Upvotes: 0

arsenius
arsenius

Reputation: 13306

Continuing with axiixc's comment, here is some code that you might use to place a button at the bottom of the webview. By putting the positioning code in layout subviews you can handle rotation properly.

- (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;    
}

Upvotes: 0

Boris Oks
Boris Oks

Reputation: 106

You can inject html markup such as <a href='yourTag01'> <button>My Button </button> </a> into the content of your UIWebview using stringByEvaluatingJavaScriptFromString method.

To capture the click event and prevent it from reloading content of your webview use this delegate:

- (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;
}

Upvotes: 9

Joshua Weinberg
Joshua Weinberg

Reputation: 28688

You can't do this in any sane straightforward way. You may want to reevaluate what you're doing.

Upvotes: 5

Related Questions