Objective-c ViewControllers and Views - uiview

I know this has been talked about ad nauseam on here but I'd like to get some perspective from other developers:
If I have a view controller with a view and then I add subviews to that view, does each subview need its' own viewcontroller or can the first viewcontroller also control the subviews?
So
UIViewController
UIView
UIView
UIButton
UILabel
UIView
...
the subviews do not take up the whole screen - they are for the most part the width of the screen but maybe 200.0f tall or less. They display information for a sales tool app.
The rule of thumb I was presented when I first started iOS development was if the view took up the whole screen, then it required its own viewcontroller, if not make it the subview of a viewcontrollers.view
Just wondering what the accepted approach is.

The View controller can access the views that are created in XIB, just create IBOutlet properties for the views in view controller source files and link the views to file's owner in interface builder.
Your rule of thumb is correct.

Related

UIStackView as a root view of a XIB

I'd like to create a small UIViewController that contains an UITextField and a UILabel going one after the other.
I've decided to use an instance of UIStackView to lay out the subviews.
However, I have to add a UIStackView as a subview of the parent UIView in XIB.
Is it possible to configure XIB the way that the root view of the controller would be of a certain class, eliminating the need for the extra view?
I've attached the current view hierarchy. I'd like to get rid completely off the topmost View.
Yes you can, you need just to set the stackview as the the root view of your view controller as following:
you can do the following:
1- create new file -> under the user interface choose "Empty"
2- then you will get an empty XIB
3- after that you can add your UIStackView from the UI Component picker
4- put your UITextField and UILabel
5- finally create your .swift file and wire it up with your XIB

How to resize custom (created in separate file) UIView height on the ViewController which uses it?

I have a custom UIView (consisting of .swift and .xib files). There is a button inside of it which changes its height.
I'm using this custom UIView some of my ViewController. To do it I drag UIView on the ViewController and set its class to my custom UIView. This also allows me to use this button and "change" displayed size of my custom UIView.
However the size of UIView which contains my custom UIView doesn't change and I cannot use GMSMapView which lies under this view.
How can I solve this issue and change actual height of this view in the ViewController too?
The easiest way is to create an outlet of a constraint from storyboard and adjust its constant-property. That will course the view to resize.
Its an unusual behavior that a view resizes itself. The SuperView or ViewController should manage things like that.
When creating an outlet of the constraint, you should consider creating the outlet within the ViewController and not within the view.

How to implement custom UIView which is scrollable/zoomable?

I want to create a "timeline" view. I'm comfortable with the GraphicsContext drawing API and using it in the drawRect: method. What I'm unclear of is the right/idiomatic way to make this view be scrollable/zoomable.
Is it more common to a) subclass UIScrollView or b) embed my custom view in stock UIScrollView placed inside storyboard?
Depending on which I use, how do I communicate my "desired bounds" to the scrolling machinery? And how do I make my drawing code aware of what the current scale/scroll is so I can draw appropriately.
(I did google for this, and haven't found any recent and simple recipes for this)
Don't subclass or embed. Set a UIScrollView as the outlet of your viewcontroller. UIScrollView inherits from UIView so you can treat it just like the normal UIView outlet of your view controller, plus you can scroll it.

UIView overlaps UISegmentedControl

In my app, I want to add a UISegmentControl on top of a UIView.They are siblings of a parent UIView.I pull a UIView to the canvas from object library first, and then pull a UISegmentControl second,but unluckily the first added UIView overlaps the UISegmentControl. What I want is that UISegmentControl is on top of the UIView. I mean UISegmentControl z-index is higher than the UIView.
The following is the screenshot.
One potential solution would be to programmatically send either the UIView to the back or the UISegmentedControl to the front in viewWillAppear(animated:) using parentView.bringSubviewToFront(segmentedControl) or parentView.sendSubviewToBack(otherView). It doesn't solve the issue of the incorrect appearance in your storyboard but it ought to fix the issue once the app is running.
1) First reduce the width and height of the overlapping view to understand its location in view hierarchy. Share your view hierarchy here so we can see in detail.
2) Delete everything from storyboard. Add UIView and then add any subviews. These 2 controls should be children of UIView in view hierarchy.

Add UIScrollView to UIViewController

I am trying to add a UIScrollView to an existing UIViewController (with navigation and tab bar) using storyboard and autolayout but I don't get this to work. Up to now I have added all components to the controller by dragging them on it. Now I try to group them in a UIView, so that I can make this UIView a subview of UIScrollView. When I drag my existing components as a subview of my newly created UIView the position of it is wrong. So I must manually correct all positions. Afterwards the compoents are 64px below their old position.
I just cant get it to work. Is there a tutorial or something how to add an UIScrollView to an existing storyboard?
This is the easiest way to learn how to control a UIScrollViewer within a Storyboard.
http://agilewarrior.wordpress.com/2012/05/18/uiscrollview-examples/
You might want to deselect Ajust Scroll View Insets in interface builder.
You can also set in your viewDidLoad :
self.automaticallyAdjustsScrollViewInsets = NO;

Resources