UIView: which method is called just before being shown? - ios

I would like to make some operation inside a custom UIView, based on its width.
Which UIView method is called just after the UIView dimensions have been defined and before the UIView is ready to be shown?

The method is layoutSubviews: of the UIView class

Related

Objective C - Add UIView to superview of UIView custom class programmatically

I have an XIB for my UIViewController. Above my ViewController I want to add a UIView Custom Class.
Now I need a dark view between the UIViewController and my Custom UIView.
I wanted to implement the Dark View inside the UIView custom class using
[self.superview addsubview: _darkView]
I tried to do this but it doesn't seem to work ...
You can tell me how to solve this thing programmatically by working only from my uiview custom class ? Sorry for the stupid question but I can't solve this thing
You can try to add subview on window
[self.window addSubview: _darkview];
Let me know if it works for you or not.

Using a subclass of UIViewController subclass with subclass of UIView subclass

I have an MVC architecture of a certain view within my app. Now I want to create a subclass of the View and Controller part. The original UIViewController subclass is loaded with a UIView subclass from storyboard. How can I make my subclass of this UIViewController subclass use my subclass of the UIView subclass code when it loads its view?
EDIT
Here's some more details about what I want to do. I currently have these classes:
ControlView
ControlViewController
which are connected with storyboard. I load an instance of ControlViewController using:
self.storyboard!.instantiateViewControllerWithIdentifier("ControlViewController")! as! ControlViewController
Now what I want to do is subclass ControlView so that I can change some constraints and add a few extra subviews. Let's call this subclass ExpandedControlView. I also want to subclass ControlViewController because I want to add some extra actions from ExpandedControlView to the controller. This can be called ExpandedControlViewController.
Then I want to be able to instantiate ExpandedControlViewController and use ExpandedControlView like ControlViewController would with ControlView.
In storyboard, select the hierarchy view.
Then select your UIViewController's View in the hierarchy on the left, and select that view's class in the identity inspector on the right.
Or if you want to do it in code. Override viewDidLoad on ControlViewController, instantiate your custom view and set your ControlViewController's view property to your custom view.
-(void)loadView{
ControlView *myCustomView = [[ControlView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];
self.view = myCustomView;
}
One caveat when doing this is that the layout of any subviews might change from the time loadView is called. So if you have subviews in your custom view, you should also override layoutSubviews in your custom view and set all your view's subviews' frame property again there.
Implement loadView in your view controller subclass and set the view property with instance of your custom view subclass.

what is the difference between subview and subclass

Well, the title says it all. What is the difference (if any) between subview and subclass? Is a subview also a subclass? e.g. when the app launches, the main View is a subview of UIWindow, but UIWindow is a subclass of UIView.
The UIWindow is a subview of UIView becuase the UIWindow is a subclass of UIView. The same concept holds true in other object oriented languages. I can have a super class called Vehicle with subclasses car and motorcycle. The car and the motorcycle are 'subVehicles' of Vehicle...just like UIWindow is a subclass and therefore 'subView' of UIView.

Draw to UIView subview using an instance of that subview

I am trying to draw to a UIView subview from another class.
MainViewController with a view that I have dragged a UIView into in IB.
I have created a DrawView class and changed the UIView's(the one I dragged onto the MainController's view in IB earlier) class to DrawView.
I can draw to that DrawView UIView subview by coding in DrawView so that works as expected.
I have a custom UITableViewController class and would like to draw in the "DrawView UIView" from the custom UITableViewController. How to do so?
I have tried creating a new instance of DrawView from the custom UITableViewController and creating functions in DrawView that I can call to draw. Doesn't work because my created instance of DrawView is a different instance than the "DrawView UIView" in IB that is created on launch. The newly created DrawView's bounds are zero, too, passing in bounds for initWithFrame doesn't work.
I don't understand how I can get the same instance of DrawView created in IB to use in the custom UITableViewController.
In case the above is too wordy, I want to call drawing methods to draw in a UIView subview in its class from another class.
How can I implement this? Thanks.

iOS: Detect when my UIView is add in other view

CustomView *customView = [...];
[self.view addSubview:customView];
I need to detect in my CustomView class when it is added in other views or when my superview changes.
You can use willMoveToSuperview: and didMoveToSuperview to detect when the view is moved around. layoutSubviews will be called when the superview changes frame.
For a UIView use - (void)didMoveToSuperview
For a UIViewController use -(void)viewWillAppear:(BOOL)animated
also assign THE TAG of Customview before addsubview and the detect by Particular TAG.

Resources