Only subviews added in viewDidLoad automatically resize - objective-c

I've created a custom subclass of UIViewController that acts like a UINavigationController or a UITabBarController. Let's call it a ToolbarNavController. It has a toolbar at the bottom with controls for the user to move to a different content view.
I have two content views aside from the ToolbarNavController's view. Both are loaded from a nib and have their own controllers. The app starts out showing one of them. A button in the toolbar allows the user to switch between them.
When I add these views as subviews of the ToolbarNavController's views in viewDidLoad, they are correctly resized to fill the area between the status bar and the toolbar without overlap/underlap.
But when I try to lazy load the second view, adding it as a subview for the first time only when the user presses the toolbar button, iOS does not resize the view to account for the toolbar in its parent view, and it underlaps the toolbar which messes up my Autolayout constraints. Also, when I don't add the subview in viewDidLoad, if I put the device in landscape orientation before switching to the second view, it loads with a portrait orientation frame.
Bottom line: When inserting a subview in viewDidLoad, iOS sizes it correctly and manages autorotation for it. When inserting it later, I need to detect orientation set the frame myself. (And for some reason when I do this, autorotation kicks in again).
What is going on?

In viewDidLoad, the view is not yet layout for the resolution and interface orientation, and view properties are as they were in the interface designer. So, if you had a portrait view, that is how the initial properties of the view are set when going into viewDidLoad. When you add your view there, you add it to the XIB view. Later, iOS performs layout on the view hierarchy and thus resizes your inserted view as needed. But when adding your view at a later point, the view hierarchy has already been layout, so it is expected that the new view you are adding is also layout correctly.
Best practice is to calculate the size you need using the size of the view you are inserting into. For example, half the width of the containing view, or third the bounds, etc. This way it is independent on the orientation the interface is in.

Related

How to pan down a view controller in storyboard

I'm using a scroll view in the storyboard for one of my view controllers and I would like to know if there is a way to move the current view of the view controller down so that I can add things below.
I've seen that when tapping on CollectionView (which is at the bottom of my screen and extends below the view), the view on my viewcontroller seems to move down a bit to reveal more of the CollectionView, but not entirely. Is there a way that I can make the view go lower?
Change the size of viewController as shown in the image below:

How do automatically resize an NSPopover with auto layout

I currently have an NSPopover subclass which sets it content view controller to a custom NSViewController meant to represent a tab view:
self.popover.contentViewController = tabViewController;
self.popover.animates = YES;
I'm rolling my own "tab view controller" because I've heard that NSTabViewController doesn't play well with animations. I'd like to use auto layout so I don't think I want to mess around with the popover's contentSize property. When I change tabs the popover correctly changes its size, however, it doesn't animate the change. Furthermore, I have a cross-fade animation that occurs when the tab view switches and the popover doesn't resize until after the animation finishes.
First, I'd like to figure out how to get the popover resize to animate and then I'll worry about getting the animation in sync.
Thanks

UiButton click doesnt work on UIView

I have a UIView which is added to Page View Controller. This UIView have a multiple buttons which are tied to an action defined in the controller. At runtime, these views get added to Page View Controller. The problem i am seeing is after loading the page view controller, when i click on the first button. It works fine. But the rest two buttons doesn't. I made sure that the User Interaction is enabled and it is linked to IBAction. Don't know what is causing this issue.
UIButtons are usually unresponsive if their superview userInteractionEnabled property is set to NO or if they are outside of their superview's bounds. The fact that only two of the buttons are not working, makes me think that the latter is the cause of the issue. Set the background color of the superview, run the app and check that the buttons are within their superview's bounds. If they aren't, move them inside the bounds or increase the superview's frame size in order to fit them in.

How to resize sub views in Navigation Controller in Interface Builder

I'm building an iPhone application and in one of my problems, I am trying to resize sub views. First, I have a Table View that when each data is tapped, it opens a new view. But the problem is that images and text in that opened view (which contains a Navigation Controller) is not aligned properly. They are all misplaced and my guesses is that on that view, it's only showing 3/4 of the top, not the whole view (which most of my images at the bottom are only showing half of the image). My goal is to fit every image and text to fit into my view that contains a Navigation Controller at the top of my view. So after researching, I can change the size and position in Interface Builder, but for some reason they are blank which I can't edit:
In addition, I tried this code in my viewDidLoad:
// DetailViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.view.autoresizesSubviews = YES;
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
So far none of these are even resizing my view to fit with my navigation controller, so I hope someone has a better idea how to resize and fit my view with Navigation Controller, thanks
Let me summarize, so I get what you're trying to do here. You have a view with a UITableView. When you select a cell in that table view, you push a new view onto the navigation stack. In that new view, parts of your layout are obscured at the top by the navigation controller's nav bar?
Assuming I'm right...
You're issue is with the autoresizing mask of the view (and subviews) of the view that is being pushed onto the navigation stack. You'll need to set it in Interface Builder to see the correct results. It should look like this:
This means that the view will shrink and grow to match any size constraints that it finds itself under. In the case of being shown in a UINavigationController, it will shrink itself so that it fits in the smaller screen space between the nav bar and optional toolbar.
Take care to also set appropriate autoresizing masks on all of the subviews of this view as well, such that they all handle arbitrary resizing appropriately.

Programmatically change subviews from within subview's controller

In my iPhone application I've set up a default, blank view called Main View into which various child subviews will be loaded for different parts of the application. It's the same approach as if I was using a tool bar to switch between subviews. That case, in the MainView controller I could hook IBActions to buttons in the toolbar, so that when a button was pressed, MainView added different subviews to itself.
In my situation, though, I need to tell MainView to change its subview from within the subviews. So here are two sister subviews, each with their own controller and xib, that would be loaded as subviews of MainView:
- StartView
- FormView
In StartView, after some animations and welcome stuff, a button triggers the camera image picker. Once the image picker returns the image, I need to tell MainView to remove StartView and add FormView.
It may be the result of a long day or my newness to iPhone OS but I'm stuck getting my head around the right way to set up my objects/controllers.
You never have more than one view controller active at a time. (The nav and tabbar controllers don't control views, they control other controllers.) In this case, you will have a single controller that has the MainView as its view property. It will add StartView and formView as subviews of MainView.
However, this is not a good design. It will overload the MainView controller by forcing it to juggle many views. It would be better to use a hidden navigation controller or a tabbar. Hierarchies of controllers can create the illusion from the users point of view for almost any interface layout you can imagine. There is no need to create a logical structure that mimics the visual one.
From your description you may only need a single view/view-controller pair: Set the formView controller to open the camera view before it displays the formView. When the camera is dismissed it reverts to the formView automatically. No fuss, no muss.