Passing an existing UIWebView to another view for usage? - ios

I have a bunch of UIWebViews in the cells of a UITableView. When tapped, I'm opening up a new view and recreating the UIWebView in the cell.
Obviously it would be greatly advantageous if I could use the webView instance that already exists, saving the user from opening the web page twice.
However, when I tried passing the webView and adding to its new parent UIView object, the webview seems to have issues resizing itself. Its simply not adjusting its height and width, even though I explicitly adjust it and confirm the new frame size. Is it not possible to resize a webview's frame? Or am I missing something?
Thanks

I'm almost positive this is possible. I've done it with custom videoPlayerViews, passed them from a collectionViewCell to a viewController.
Some ideas:
Make sure that you remove the webView from its original superview before adding it as a subview of its new superview.
Make sure your autoResizingMask and constraints are set up properly with respect to the new superview, and destroyed with respect to the original superview.

Related

When I drag any objects into autolayout in xcode, it automatically goes fullscreen and I can't resize it

Is there a setting I accidentally checked that would force this behavior? Even button objects occupy the entire screen.
I figured it out. I accidentally deleted the view object from the view controller. The view objects acts as a container for other objects and "handles the rendering of any content in its area and also handles any interactions with that content." You just drag and add the view object back in autolayout and everything works again.
It's weird behaviour. Maybe fixed in new avaialble xCode. Anyways, after dragging an object, click on the object and goto Size Inspector and change the width and height along with X,Y if required. The size will be reduced and you can make use of it.
For each object you have to reset the layout rules. Or disable the autolayout from your storyboard (look at the image)

Xcode Interface Builder Height/Width Settings vs. View Controller

I'm having a hard time wrapping my head around how frame properties (height, width, posX, posY) work in terms of setting them in a View Controller vs on the Storyboard (Interface Builder).
For instance, let's say I have a UICollectionView object that I set to have a width of 400 and a height of 800. Then, in my code, I set the frame of that same object to 600 x 400. I haven't really found a consistent behavior. I tried setting the frame in viewDidLayoutSubviews and it sort of worked - but it seem to 'jump' back and forth between that and what was set on the storyboard.
Basically my question is, when do the properties on the storyboard change the UI object? I assume that I just need to know that, and then reset them in the View Controller after the fact. Or, is there a way to set the height and width empty so that I can do it all in the code?
Any insight into this would be very helpful!
If you use AutoLayout in your project then setting frames of the view objects you configured in storyboard won't work. Because after you set the frames, AutoLayout will update frames again which makes the frames set by you not working. If you want detail, you can check this article:Advanced Auto Layout Tools But you can set frames of view objects created programmatically to position them.
You can check if you have turned on AutoLayout in you storyboard file's file inspector. There is one thing though, if you do want to use AutoLayout, be sure to not set view's translatesAutoresizingMaskIntoConstraints to NO. The default value of this property is YES. If you use AutoLayout, this property is set by storyboard for you, because constraints created in storyboard are enough to layout the views.
EDIT:
1) if you are not using AutoLayout then setting frames in code should work as expected.
2) yes you can, a little tricky though. you must create UICollectionView yourself using [[UICollectionView alloc] init] or load it from nib. and then configure cell in IB with a xib file. you can use AutoLayout to layout subviews of cell in xib file. and register the class of cell to UICollectionView or load cell object from nib yourself. then you should calculate the size of every cell and let AutoLayout layout subviews of cells.
although this is easier than layout interface entirely in code, it's still a little complicated. the better way is using AutoLayout. Since not all the layout detail can be done in the design time since some views' frame may be different depending on data. you can make a basic layout with AutoLayout first, then IBOutlet the constraints you want to configure on the fly. and change the constant property of constraint objects later. this way, you can 100% control the layout process and also let AutoLayout do the dirty jobs you don't want to do yourself. I suggest you read official docs of AutoLayout and other good resources about it. The learning curve is steep at first, it may make you want to kill yourself too. But it's really powerful and easy to use. once you figured out how AutoLayout works, it will make your iOS development life much easier.
If you want to set the size of things through code, you could try creating outlets from the storyboard to the View Controller. Then in the View Controller, you can use viewDidLoad or viewDidAppear to set the size properties of your object(s).
viewDidLoad will get called when that view is first created. viewDidAppear will get called each time that view comes back onto the screen (like if you are going back with a navigation controller).

How to automatically fit the parent view to its child views programmatically?

I'm new to IOS development and I'm trying to make one of my parent view become automatically fit to its child views.
For example the picture above have a red circle which indicates a blank space I'm going to adjust the text upward so it fulfill the spaces and I also want that the container of all the text will automatically adjust its height to fit for its content.
Thanks.
You need to override the willMoveToSuperView and didMoveToSuperView
Check the answer here
iPhone/iOS: Will there be called any method if a UIView is added as a subview
You can then adjust the view according to its super view. However, using auto layouts is a much preferred way.
You Should do one thing.
First alloc your parent view and give it a frame then, when your child view will alloc and it has a frame then set your parent view's frame one's again.
So, when your child view will appear then your parent view's frame will automatically set.
Hope this helps...

iOS: How can I prevent a resized view from resetting itself?

I'm using ECSlidingViewController. When the app starts, it opens a scroll view which is resized dynamically, since it contains both labels and a table view. Both the table and the scroll view containing it are resized.
Everything works as intended when the view is first loaded. However, if I open and close the menu view, or if I use it to go to any view which resizes itself, the resizing does not work. Instead, it displays the scroll view as if it has the height assigned to it in the storyboard.
I'm completely lost as to why this happens, and would greatly appreciate help. I can post code as well, of course, but I don't know what code might help, since I guess that the error is on ECSlidingViewController's part.
Are you using auto layout? If so, whenever something else happens that triggers the application of the constraints, all the frames will be reset to the values dictated by those constraints. Try turning off auto layout and see if that fixes it. Refer to your auto layout settings.
Alternatively, if you want to keep auto layout, I'd generally suggest changing the frames by programmatically changing the constant values of the constraints. But, I'm not familiar with ECSlidingViewController, so I don't know if that's a reasonable option in this particular case.

UIScrollView unwanted scrolling after addSubview or changing frame

I have a UIScrollView filled with subviews, all is well when creating it and initially filling it.
But when I add a new subview that is positionned outside of the visible screen portion, or when I just resize an existing subview that is also outside of the visible screen portion, there is a subsequent 0.3s-long scroll animation (I can see it happening from my delegate) that seems to match the newly added/resized element.
Attempts:
pagingEnabled is always NO.
Setting scrollEnabled to NO during subview manipulations doesn't help.
Doing a setContentOffset:animated:NO after subview manipulations doesn't prevent the animation.
One single giant subview with all my subviews in it doesn't help.
My current workaround is to initially set the frame to fit inside the visible screen portion, or doing resizing work inside another superview, but it feels dirty, and won't handle all situations...
Is there a way to prevent this automatic scrolling animation when programmatically manipulating subviews?
Xcode 4.3, iOS SDK for 5.1.
I too discovered this problem and found this solution http://www.iphonedevsdk.com/forum/iphone-sdk-development/94288-disabling-uiscrollview-autoscroll.html
It involves subclassing the UIScrollView and entering no code in the following method.
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated {
}
Like the guy says on the link I've found it works and no problems so far. Hope it works for you.
I had this problem because I set the content size of the scroll view prior to adding the subview.
As soon as I change the code so that the content size of the scroll view was set after adding the subview the problem went away.

Resources