Krumelur
Krumelur

Reputation: 32507

Custom UIView for HTML element in UIWebView

I would like to add a custom view to the layout of an UIWebView, so that the HTML/JS/CSS describe the layout of the "page" with my custom controls in it. Ideally, it would be possible to place e.g. a special element that triggers a callback into my code that would allow me to place my own UIView derivative on the specified coordinates (and it would of course scroll and zoom with the parent UIWebView) and with the specified set of parameters.

A poor man's choice would be something similar to this question: Getting the position of an HTML element in UIWebView however I am not sure how reliable that would be w.r.t layout changes.

Upvotes: 3

Views: 1241

Answers (1)

mackworth
mackworth

Reputation: 5953

Well, in addition to getting to coordinates from the SubView as described in your link, the only other trick is to attach to UIWebView's ScrollView, which happens to be the subview of it, usually number 0 unless you've done something odd. Then your view will scroll and zoom with the rest of your webpage. Assuming you get these pieces to work correctly, you should post your resulting code. It'd be interesting to see.

For example, attaching a button at 150, 200

    //Make a button
    UIButton * myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton.frame = CGRectMake(150, 200, 50, 30 );
    myButton.titleLabel.text = @"Press me";
    [myButton  addTarget:self action:@selector(pressed:) forControlEvents:UIControlEventTouchUpInside];

     [[[self.myWebView subviews] objectAtIndex:0] addSubview:myButton];

Although for robustness, you might want to verify that self.myWebView has a subview and that it is of a UIScrollView class before adding to it.

Upvotes: 3

Related Questions