UIStackView as a root view of a XIB - ios

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

Related

Not able to set constraints in XIB - Xcode 11.3.1

I created a project in Swift. I added view controller file and one XIB using right click on the project - > Add Files to projectname -> Empty (xib) file.
After adding the xib file, i removed the view and added View Controller. Under the View controller , i have added few button. But i am not able set the constraints.But if i add the View object instead of View Controller, i am able to set the constraints. I don't understand why the xib constraints are disabled for View controller. Attached the screen shot
Layout is set to Translates Mask Into Constraints, meaning that IB is gonna generate the constraints for you automatically.
Set it to Automatic and it'll work.

Not able to add footerView in UTableView through XIB

I am trying to add FooterView(UIView) by dragging down on UITableview through XIB but can't.
Yes we can add it programetically (tableView.tableFooterView = myView) & also works with dragging view in Storyboard,
But I want right from XIB.
You cannot do this in a Xib. If you think about it, a storyboard is somewhat like a collection of Xib files. Thus, in a storyboard you can add a footer view as a kind of 'xib' embedded in that storyboard. A pure xib cannot contain multiple child xib's, so what you want is not possible unfortunately.

Can I create a custom UIView from a .xib and then use that view for my UIButton?

I need to refactor my UI where I had a UIButton with two labels placed on top of it and constraints set in order to display it properly.
What I have done is created a custom UIView that looks exactly like my previous button but now with the labels contained within the view and constraints inside the .xib instead of in the storyboard where they were placed.
I want this new view to be used as the buttons view; is this possible?
If not, is the way to make my custom view "like" a button is to used touchesBegan/touchesEnded? How do I handle the touchDown highlight of the system UIButton?
Thanks!

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.

Objective-c ViewControllers and Views

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.

Resources