Can I set the width of UIWebBrowserView? - ios

I have a UIWebView on iPad and its frame is CGRect(0, 0, 540, 620).
When it opens a page it appears like this.
The web content is large than my frame. (web content in blue border)
I found the view in blue border is UIWebBrowserView.
So, can I set the UIWebBrowserView's width via my web view?
Or, is there other ways to make my web page content to fit my web view size?

You either need to zoom out the content (using the scrollView property of UIWebView) or set an appropriate meta tag with a correct viewport, so that the content appears in the correct size.

Related

In Swift, how to determine the physical size of a device screen?

I am adding views programatically. For example, I have a scrollView as a top-bar menu, I set its height to 60. (I am already using auto layout for placing these views)
This top-bar menu looks alright visually in an iPad. However, in an iPhone, it's kind of occupying too much space. Therefore, I am thinking to change the height to 45 for an iPhone.
What's the way to get the physical size of the screen? (not screen resolution) So I can make the height proportional to the physical size?
You can get this information accessing UIScreen singleton.
To get size use UIScreen.main.nativeBounds. Also you can get scale coefficient of screen UIScreen.main.nativeScale.
You may want to consider working with the frame property of the view of the current view controller.
self.view.frame.size.height and self.view.frame.size.width
You can make your other views proportional to those dimensions or based on their values, etc.
This code should adjust your scroll view to the size of the users screen.
self.view.frame.size.height and self.view.frame.size.width
Also, make sure that your constraints are all good.
Use view.frame.size.height and view.frame.size.width to determine the size of different screen sizes. This will work inside functions only.

WKWebView+contentInset makes content size wrong

On iOS, when you set a contentInset on a WKWebView's scroll view, it seems to make the web view think the content of the page is bigger than it is. For example, if you give a mostly-empty page a top or bottom contentInset, you'll be able to scroll the page down even though there's nothing to scroll to.
Is this expected behavior? Is there a workaround that still allows me to use contentInset?
The problem is that WKWebView always seems to use its frame as its viewport size, neglecting to subtract the scrollView's contentInset. I was able to find a workaround by stephan-leroux.
The idea is to set the WKWebView's frame to the desired viewport size, either manually or using autolayout constraints. To restore the "scroll-under" effect of contentInset, disable the scrollView's masksToBounds:
wkWebview.scrollView.layer.masksToBounds = NO;
weiyin's suggestion (resizing the web view's frame) worked for me.
Just wanted to share a code example:
CGFloat topInset = ...;
web.scrollView.layer.masksToBounds = NO;
[web.scrollView setContentInset:UIEdgeInsetsMake(topInset, 0, 0, 0)];
[web setFrame:CGRectMake(0, 0, web.width, web.height-topInset)];
Four years later and the problem still persists...
weiyin's answer works great, but if you're like me you might struggle a bit with figuring out what exactly to do (though it's really quite simple).
So here's a simple explanation for dummies (like me):
Embed your Web View inside a Wrapper View.
Size and position your Web View in a way that its frame is inset from the Wrapper View precisely by your desired content inset.
Set clipsToBounds = false on the Web View's scroll view (and true on the Wrapper View).
Make sure that there is no content inset set on the Web View (webview.contentInset = .zero).
(This is the default and normally, you don't need to set it at all).
Done:
100% content inset behavior on the Web View without using content insets.

UIWebView in A UIscrollView

I'm trying to add a UIWebView in a UIScrollView in storyBoard. I disabled the scrolling for the webView and enabled it for the scrollView . Since there are others views in the scroll view, i don't want to split the parent view.
But my problem is , the webView takes a static height even though I didn't set a height constraint for either of them (the scrollView and webView).
This means that the webView content didn't exceed its canvas(then its extra content got cut) and the scroll view didn't have extra space so it is not scrollable too .
What best I understood with your question I am trying to answer.
You have problem that after stopping web view scrolling you are not able to view web view contents. So you can do two things,
1) As your scroll view is having other subviews also,don't stop web view's scrolling but set its frame constrains so that with in a frame size you are able to view all web view content.
2) You can place web view in the bottom of scroll view and according to web view content set scroll view's content size. Set web view's bottom constrain to 0 with its parent so that there is no need to set web view's frame again and again.

Setting correct sizes when using UIScrollView and setContentSize

I simply just want to have more controls than I can fit on the normal iPhone screen. Take for example viewing someone's contact information in the Contacts app.
It appears that I should use a UIScrollView to implement this.
Lets say I need about 800 pixels for this. So in my NIB, I set the View of the ScrollView to have a height of 800. I then add my controls under the Scroll View.
I make my connection, and then in the viewDidLoad, I do the usual:
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 800)];
If I build and run at this state, it wont scroll. So looking around online, people say that you have to set this to be larger than the UIScrollView.
I can't seem to find information on how to actually calculate what that 800 should be in the setContentSize. Is it just guessing until it looks right? If I set it to 801, I never see everything at the bottom of my Scroll View, and if I set it to 1000 I can see the bottom, but it doesn't really end at the bottom.
I just want that if the user scrolls down, it ends at the bottom correctly.
How is this calculated? Or am I missing something? This seems like a rather common use but haven't found code examples that simulate this simple example.
Scrollview provides a perspective onto a bigger canvas by showing a smaller region but allowing you to pan across the canvas. Now when you set the scrollview's frame size and the content size to be same, the purpose is defeated. Hence no scrolling.
When you define the scroll view's frame to be 320x800 on a window/view that is of size 320x480, part of the view is clipped off. It is not visible but its there. You content is there too. What you need to do is to set the scroll view to match the window's size i.e. 320x480. Now you will have scrolling as the perspective is smaller than the canvas.

Can I create a UIPageControl / UIScrollview where the pages are larger than the screen size?

I am writing an App where we want to have several pages in a UIPageControl/UIScrollView. The problem is I need some space between each page - in other words, scrolling from page 1 to page 2 there is a visible gap between the pages.
I've tried doing this by setting the page's frames to be larger than the UIScrollView, but I can't seem to get this working. Does anybody know if it can be done?
The page size is always the same as the width of the scroll view. To make the pages larger than the visible area, you should make the scroll view itself a little larger, too. For example, if the width of the screen is 320 points, make the scroll view 340 points wide (10 points overhang on each side).

Resources