Does a hidden UIWebview layout or paint a webpage? - ios

Does the iOS operating system prevent the DOM layout and painting of a webpage loaded in the webview if the given webview is hidden in the background (of the app)? Say, the webview is part of a view controller that is not made visible yet but I've asked the webview to load a URL.

The system doesn't draw nor layout any view that is not part of the view hierarchy. You can easily verify it by subclassing UIWebView and overriding drawRect and layoutSubviews. That said, in almost all cases, you shouldn't have to care.

Related

ios - Adding subview to WKWebView below JS Dialog

When adding subview, something like [self.webView.scrollView addSubview:test]; everything works fine.
Problem is javascript alert and it's dimmed screen, because alert is below my newly added view. JS alerts and dialogs should be always on top.
Any ideas?
Javascript alerts from the web view are part of that web view and not separate UIViews.
If you want something to appear above the main web content and below the javascript alert (which is also web content), then it will need to be web content itself.
One way to do this would be to make it another javascript-generated object, not UIView.
Alternatively, you could have a second web view that only displays javascript alerts, and have your UIView appear below your new (second) web view, but then you'd have to manage the relationship between the two web views.

How to create banner images that pan across screen using Xcode/Swift?

How do you create a banner (say, above a UITableView or something) that will switch images after a given time, or upon swipe. It then recycles through after a set number of images. See the below provided example from the IF app).
Also, I'm not necessarily looking for code here, but simply general features of Xcode I'd use to make it happen (ie, UIImageView within a ContainerView with gesture recognizers or something). Any ideas?
Here's the first image:
Then the beginning of the transition:
Then the second image in place:
This is an example of how to get that exact behavior using UIPageViewController. From the documenation:
A page view controller lets the user navigate between pages of content, where each page is managed by its own view controller object. Navigation can be controlled programmatically by your app or directly by the user using gestures. When navigating from page to page, the page view controller uses the transition that you specify to animate the change.

iOS swift: swapping between two webviews

I have a view controller that has a webview whose content is injected. I want to be able to preload/prerender the content before displaying the webview.
My solution was to create two webviews instead and swap which one is displayed. How do i do that?
I tried swapping the reference to the second webview, remove the first from superview and add the second to the superview. But nothing is showing up
If you want to pre-load your webviews, they need to be instantiated. If the webview isn't added to a parent view it may not load correctly. I would try keeping both webviews in the layout and just setting them hidden where appropriate. Setting them hidden will hide them from the user, but they will still behave as expected.

iOS: setFrame no longer working from viewDidLoad or viewWillAppear

I prefer to disable autoresizesSubviews and to use setFrame to place all of my subviews.
As of iOS 6, things seem to have changed a lot.
When I call setFrame on a view in viewDidLoad, there is no effect. I tried it from viewWillAppear; same thing.
A setFrame call will work in willAnimateRotationToInterfaceOrientation, but that is not called initially like it was in iOS 5.
Can someone clarify please where I am expected to layout my views from?
I guess you want to layout your views from UIViewController. Have you tried performing your layout tasks in viewDidLayoutSubviews:
- (void)viewDidLayoutSubviews
According to Apple's documentation:
Your view controller can override this method to make changes after the view lays out its subviews.
Since your view itself does not do layout its subviews (you do not use autosize, autolayout or layoutSubViews:) you can do the layout tasks in this method.
Nevertheless, the elegant way would be to use a custom parent UIView and perform all the layout there, overriding UIView's layoutSubViews: (unless you add/removes views dinamically). Quote from Apple's documentation on "How View Controllers Participate in the View Layout Process":
Ideally, the views themselves perform all of the necessary work to reposition themselves, without requiring the view controller to participate in the process at all. However, if the view controller adds and removes views dynamically, a static layout in Interface Builder may not be possible. In this case, the view controller is a good place to control the process, because often the views themselves only have a limited picture of the other views in the scene.
You can use setFrame in viewDidLoad if you uncheck the Autolayout option in Interface Builder. If you need to use auto layout, You need to perform your layout tasks in viewDidLayoutSubviews:.
Regarding "Asking the views to lay themselves out doesn't make sense to me. Views don't have authority to say where they should go. That's the role of the controller, isn't it?". I think the answer is often negative. It's the parent view (not view controller) which usually plays the role of laying out it's own subviews. It does so by utilizing autoresizingMask or autolayout (iOS 6 only). Or You can make layout programmatically by overriding -layoutSubviews method of the parent view.
Sure, like Imre mentioned, controller can participate in the layout process as well by overriding - (void)viewDidLayoutSubviews. I often use this approach when I don't want to subclass the top level view of the view controller for keep things simple.
Finally, I found this post super useful. Even with some minor errors, the post provides a big picture of view layout process.

How to expand UIWebView on click

I'm working on an Ad SDK for iOS and I have a narrow banner-size UIWebView that displays a complex HTML that I receive from an ad server. I don't know anything about structure of that HTML (i.e. it may change).
At some point user taps on that UIWebView, which causes the content of that ad to become full-screen, so I need to expand my UIWebView and show it in the modal view (via presentModalViewController).
Is there a way to either take my existing UIWebView and reattach it to my new modal view while keeping its state (whatever user clicked on)?
Or alternatively how can I simulate touch on UIWebView, since I could create new UIWebView in my modal view and load the same HTML, but then I need to re-create its state after initial tap by somehow sending a tap event programmatically.
Use UITapGestureRecognizer, make that WebView full-screen and move to the front layer.

Resources