IOS Custom UIView doesn't show - ios

I'm sorry but i'm still a bit dumb in UIViews and creating a custom one.
I have a custom view and a xib file that are connected through the XIB IB.
I want to add this view to the storyboard.
What you see in this picture is an empty view which i've connected to the custom view class
When launching the app - i see the exact same view without my custom view being loaded.. what am i missing ?

It is not enough to just link your xib view to the storyboard through the view class name. You should copy the view from your xib (open your xib, select the root view and copy) to your storyboard view controller (select the parent view and paste). You can even open (double tap the xib file in the navigator) your xib in a new window and do a drag drop to the story board. Note that all the property wirings and constrains that you had defined in the xib will be retained when you copy paste your view from xib file.

The answer is actually pretty simple.
I had a recursive init process - meaning that although i did over initWithCoder method, in the xib file, i've set the view class to be my class.
What i should have done is to make my xib file's owner to be my class instead.
And then all the loadFromNibName worked perfectly in my initWithCoder!

Related

iOS - referencing and programmatically moving a view from an XIB file

I'm using Xcode 8 and working on a legacy .xib file and need to figure out how to programmatically move all the objects. Questions:
How can I programmatically reference the view (pointed to by the red arrow)?
Assuming I can reference the view, would programmatically moving it also relatively move the four buttons and label contained within it?
Here's a screen shot of the View hierarchy of the .xib file:
Normally, I would just create a UIView object in a separate .xib file with the contents of the view from question #1, and add it the .xib file referenced above but this is existing code and I want to minimize changes if possible.
Assuming you have a view controller for this story board, Option+click your source file to open it in the assistant editor. You CTRL+Drag from the document outline (where your arrow is pointing) to the spot in your source file where you want the outlet to appear and Xcode will create an IBOutlet for the view.
If your question is can you load just the view from the story board without the view controller. I suppose you could instantiate the view controller, take a strong reference to the view, remove the view from its superview and then trash the viewcontroller, but that seems a bit wasteful; in this instance I would copy and paste the view into its own nib.
Note that as far as moving the view, yes it will move all of its subviews. If this is a temporary animation kind of thing consider using the .transform property along with one of the UIView Animation methods. You can also CTRL+Drag the constraints from interface builder and manipulate the constraints in code. You should avoid setting the frame/center manually if you are using constraints otherwise autolayout will be fighting with you.

Subclassing a View Controller

I have a single view aplicaition on xcode with several view controllers and xib files in it. I am trying to subclass a xib file's view controller called ViewControllerPg62 as a sub class of a view controller called ViewController. How can i do this?
Thanks in advance
Why are you working with XIB files? You should be using Storyboards.
I had to go back to an old project using XIBs to see how they worked.
In an XIB you should see an entry called "Placeholders", and under that, an item called "File's Owner." That represents the object that owns and manages the views in the XIB. Select that, and then display the Identity Inspector. There will be an entry at the top "Custom Class" where you can change the class that manages this view controller.
Before changing that, I suggest you create the source file for your view controller ("ViewController.swift", in your case.) Then when you change the class of the owning object, the XIB will create an instance of your custom class when that XIB is invoked.
The approach is quite similar for Storyboards, although it's a little more coherent.

Xib based view is always full-screen and displayed on top of all other views

This is a generic question and I must be missing something simple.
I've create a xib file, put a few views in it, created a view class inherited from UI view, defined initWithCoder and initWithFrame, set File's Owner of the xib to this class. The view class loads the xib via loadNibNamed. I add this custom view to storyboard to a view which takes half of the screen. The view appears correct on the storyboard, I can set constraints for it, but when the app runs, the view is actually taking a full screen. It is not appearing inside the view I've added it to.
What am I doing wrong?

Copy and paste storyboard controller into xib file. Supported or not?

I want to refactor out a storyboard controller to a separate xib, for better reusablity.
The controller extends UITableViewController.
I can select the controller in the storyboard, hit Cmd-C, create an empty xib file using the New File wizard, and finally paste the controller into the xib.
It seems to work ok. The UI elements are there, the outlets are there, etc. It forgets the orientation and the size (portrait, Retina 4-inch), but I can easily set that in the properties pane to the right in xcode.
However when trying to use this xib I get a crash with error message:
loaded the "MyController" nib but the view outlet was not set.
When I try to connect Files Owner to the table view (which is the top view in a controller extended from UITableViewController), xcode won't let me connect them.
My question: Is it unsupported to copy-paste a controller from a storyboard to a xib?
You are most likely jamming a tableViewController into a nib and there is no outlet to hook the view to. When you do Ctl-C in the storyboard make sure you copy the UITable not the UITableViewController. If not you can try to manually create the outlet in the controller but you might have to jump through hoops to make it work.

Custom UIView in Storyboard

I can't work out if I am doing this with complete retardation or not.
I would like to add a custom view to my view controller in my storyboard.
I drag a view object on to the VC and set the class to my custom view class.
I would like to then have a xib file associated with this class so that I can design a user interface for this class separately from the storyboard.
The solutions which seem to be hacks referenced on stack overflow (adding the xib as a view in initwithCoder) have not worked for me, and the prospect of programatically aligning a load of images and labels when dragging and dropping would be so much easier really frustrates me.
Is there some easier method I'm missing, what is everybody else doing?
Thanks,
Alan
With storyboard there is no xib file
hack your items to .h
press ctrl and drag a link to viewcontroller -at the bottom of ur viewcontroller in storyboard, or link it to your .h file and name it

Resources