How can i resize the parent view after changing the constraints of child views? - ios

I have a pop up view which is loading from a xib file.In that file there are couple of views, labels and buttons. For specific condition i have hide couple of views and buttons and its working but total height of the parent view of those views and buttons is not changing.
I have set the height constraints of the parent view and tried to change the constant but its not updating.
self.translatesAutoresizingMaskIntoConstraints = YES;
I want to resize the parent view after hide the parent view.
Main window
Changed window after constraints

Hiding the view does not remove it from its parents. It just changes its visibility. The view still remains in the hierarchy. To achieve what you want to do the following:
Constraint the view above and view below the view you want to hide with a lower priority.
remove the view(the one you want to hide) from its superview
I hope this helps

Related

Is it possible to move UIView outside a controller without being cut?

I have 2 UIViewControllers. One is the content an the other is a Slide menu.
And I want to move the button from Controller 2 outside its superview like this
Is that possible? Because I tried to set clipToBounds = NO for the superview. The only result I get is this:
Any suggestion?
Several options here. The most obvious is clipToBounds = NO on the Controller 2's view, and any further subview which the button is contained in.
If you do not wish to set it to NO, then the view cannot be part of the clipped view hierarchy. You then have to move it to a shared superview, such as the container controller's view, the window, etc.

UICollectionVIew inside UIView adds a huge amount of padding to the top of the collectionView

I am trying to add collection view to UIView and there is a problem that doesn't make any sense. Screenshot. I made the background color of the collection view, to make show the view area more clearly.
If I add the collection view straight to the controller (just like in UICollectionViewController), the top padding doesn't exist.
In this example, I have autolayouted the collection view to resize to the whole view but the problem existed the moment I added collection view to the UIView.
Also, if I add another collection view right after this view, there items start from the top, without any margins.
If there is anything else you need me to provide, I will do it. I think this is a storyboard bug or something because there are no insets, in the view's attributes.

Embedding a view with subviews in another parent view and keeping constraints (IB)

I have a scene in storyboard with several subviews for which I already set a lot of constraints in Interface Builder. These subviews are directly children of the view controller's view. Now I realize that I need the whole view + subviews be contained in a full-screen UIScrollView to handle scrolling of the content when the keyboard is shown. But if I simply move all the subviews inside the scroll view, I loose the constraints that referred to "superview" and I get a mess.
Is there any way to migrate the constraints regarding the previous "superview" (view controller's view) to the new parent scroll view (which is the child of the "superview")?
Thanks
No there is no way to do this. The constraints between the view controller's view and the subviews are as you know broken when you move your subviews into another subview/view. AFIAK the biggest reason for this is you could end up with subviews that have constraints which relate to views on longer in their
view hierarchy which is a violate of the constraints lay out system.

Adding a fixed-position view on top of a view

I have a UITableViewController inside a UINavigationController. I'm adding a "modal" subview to the tableView, which is a custom UIView when one of the rows is selected.
(It’s modal in spirit, not in the UIKit sense, since Apple doesn’t support modal views on iPhone, I’m adding it with a [self.view addSubview:customView] in my table view’s controller.)
I would like it to appear at the bottom of the screen and stay put there. I can get it to draw at the bottom, but once I scroll the table view, the view moves with it. Here are some illustrations:
Initial position (good):
Position after scrolling (bad):
I'm getting the bottom position by subtracting the height of all the chrome (navigation bar and status bar) as well as the height of the custom view from [UIScreen mainScreen].bounds.
How can I get the custom view to stay put? Should I constantly be adjusting its frame when the table view is being scrolled?
Your best and most flexible option is to switch to using a view controller with the table view as a subview so that you can change its frame and add sibling views. Then, when you want to add a modal you can run an animation to move the table view out of the way and slide the modal view in.
UITableViewController.view is an instance of UITableView. Means you added your custom view into a scroll view and that is why it scrolls. You can try to put your custom view into the tableFooterView property of the UITableView, which is the old school solution.
Personally I would create a container UIViewController and have UITableViewController be a sub viewController of it. Another sub viewController UIViewController or just a simple UIView could represent the footer.

When a UIView does not have a superview when loaded in iOS app?

From developer reference:
(void)sizeToFit
Call this method when you want to resize the current view so that it
uses the most appropriate amount of space. Specific UIKit views resize
themselves according to their own internal needs. In some cases, if a
view does not have a superview, it may size itself to the screen
bounds. Thus, if you want a given view to size itself to its parent
view, you should add it to the parent view before calling this method.
Is there a case when a view does not have a superview? What are those cases?
a view doesnt have a superview when the view is the rootview meaing that there's no other view behind it (besides the window possibly)
OR when it was never added to a view obviously :D
If it is your View Controller's view, it doesn't have a superview.

Resources