Resize a view when another is hidden - objective-c

I have two views on the iPhone screen, one above the other (mediaControls above deviceWebView). When I hide the top view I want the bottom view to take up the entire screen, and when I reveal the top view I want the bottom view to resize again to be below the top view. It seems very simple but I am having trouble with it.
I've tried only hiding the view as well as adjusting the layout constraints as I show below.
Heres my code:
-(void)hideVideoButtons{
self.mediaControls.hidden = YES;
[self.view removeConstraint:self.deviceLayoutConstraint];
[self.deviceWebView setNeedsDisplay];
}
-(void)showVideoButtons{
self.mediaControls.hidden=NO;
self.deviceLayoutConstraint = [NSLayoutConstraint constraintWithItem:self.deviceWebView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.mediaControls attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
[self.view addConstraint:self.deviceLayoutConstraint];

have you tried the UIView animateWithDuration: animations: method? You can animate the UIView(s) to whatever location you'd like, you can resize them, and much more.
[UIView animateWithDuration:1.0 animations:^{
// moving the device web view
self.deviceWebView.transform = CGAffineTransformMakeTranslation(xVar, yVar);
// moving the other view
otherView.transform = CGAffineTransformMakeTranslation(xVar, yVar);
}];
the xVar and yVar are not variables being set. They are just showing the usage of the method. You'd replace them with the x and y locations of the screen you are wanting to move the x and y of the view to. I'm not fully sure if this is what you're looking for, but it seems you are looking for a way to animate the views, so here's a suggestion :)
Hope this helps!

Related

Force multiplier to update on Interface Builder constraint

I've created a height constraint for a UIView in the Interface Builder that sets the height to = the superview. I set the multiplier to 1/3 of the superview's width. This works great, it sets the height, say on iPhone 5/5S, to ~107pts.
For wider screens, I want to set the multiplier to 1/4 instead; I suppose I could do this with Size Classes, but I'm not sure that's the best way to go for this?
In my viewWillAppear method of a view controller, I've tried doing the following; no matter what I do though, it still sets the height to only ~107pts. Note that _controlsAspectRatio is an IBOutlet to the IB constraint.
_controlsAspectRatio = [NSLayoutConstraint constraintWithItem:_controlsAspectRatio.firstItem attribute:_controlsAspectRatio.firstAttribute relatedBy:_controlsAspectRatio.relation toItem:_controlsAspectRatio.secondItem attribute:_controlsAspectRatio.secondAttribute multiplier:1.0f/4.0f constant:0];
// I've tried several combinations of several layout refresh options, to no avail
[_controlsContainer setNeedsUpdateConstraints];
[_controlsContainer setNeedsLayout];
[_controlsContainer layoutIfNeeded];
NSLog(#"%0.4f - %0.4f", _controlsContainer.frame.size.height, _controlsAspectRatio.multiplier);
// Output: 106.6667 - 0
Had to remove the original and add a completely new constraint:
NSLayoutConstraint* newControlsAspectRatio = [NSLayoutConstraint constraintWithItem:_controlsAspectRatio.firstItem attribute:_controlsAspectRatio.firstAttribute relatedBy:_controlsAspectRatio.relation toItem:_controlsAspectRatio.secondItem attribute:_controlsAspectRatio.secondAttribute multiplier:1.0f/4.0f constant:0];
[self.view removeConstraint:_controlsAspectRatio];
[self.view addConstraint:newControlsAspectRatio];
[_controlsContainer layoutIfNeeded];
NSLog(#"%0.4f - %0.4f", _controlsContainer.frame.size.height, newControlsAspectRatio.multiplier);

How to add uiview and make it the self view center using autolayout and without setting frame or center property

How to add uiview and make it the self view center using autolayout and without setting frame or center property.
I know that we can set like this
view.center = window.center;
or
view.center = self.view.center;
but i want to set to the center of the view to self view center using autolayout.
You should be adding 4 constraints to your view then:
One for "center horizontally"
One for "center vertically"
One to set its height to a certain value
One to set its width to a certain value
You can create constraints in code like this, and add them to your view:
[NSLayoutConstraint constraintWithItem:label
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:tab
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0];
You can do this with storyboard you don't need to write any code. Use Size Inspector and remove all the line in Autosizing part.

Autolayout of View Controller View in a Container

I have an NSWindow that has 2 container views within it, all in one xib, like so:
In another xib, I have a the view for the sidebar, managed by a different view controller, like so:
When I add the subview to the container view, I do it like this:
self.sidebarViewController.view.frame = self.sidebarContainer.bounds;
[self.sidebarContainer addSubview:self.sidebarViewController.view];
When I build and run, and resize this window, this is what happens:
The container tracks the height of it's superview properly, but the sidebar view itself does not track the height of the container.
How can I set things up so that the height of the sidebarVCs' view tracks the height of the container as the window is resized?
I think I've solved it like so:
[self.sidebarViewController.view setTranslatesAutoresizingMaskIntoConstraints:NO];
NSLayoutConstraint *w = [NSLayoutConstraint constraintWithItem:self.sidebarViewController.view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:150.0];
NSArray *c1 = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[view]|"
options:0
metrics:nil
views:#{ #"view":self.sidebarViewController.view }];
[self.sidebarContainer addConstraints:c1];
[self.sidebarContainer addConstraint:w];
I still don't yet understand why the autoresizemask layout constraint doesn't do this automatically, but I think I'm closer to understanding the relationships between the views here.
Use NSBoxes as containers. In your sidebar, add an NSBox inside the NSView and constrain all sides to the view. Then when you want to swap in a view for the side bar you can call container (the NSBox object) setContentView:.

Animate intrinsicContentSize changes

I have a UIView subclass that draws a circle whose radius changes (with nice bouncy animations). The view is deciding the size of the circle.
I want this UIView subclass to change its frame size to match the animated changes to the circle radius, and I want these changes to modify any NSLayoutConstraints connected to the view (so that views that are constrained to the edge of the circle will move as the circle resizes).
I understand that implementing -(CGSize)intrinsicContentSize and calling invalidateIntrinsicContentSize when the radius changes will tell constraints to update, but I cant figure out how to animate the changes to intrinsicContentSize.
Calling invalidateIntrinsicContentSize from within a [UIView animateWith... block just instantly updates the layout.
Is this even possible, and is there a workaround/better approach?
invalidateIntrinsicContentSize works well with animations and layoutIfNeeded. The only thing you need to consider is, that changing the intrinsic content size invalidates the layout of the superview. So this should work:
[UIView animateWithDuration:0.2 animations:^{
[self invalidateIntrinsicContentSize];
[self.superview setNeedsLayout];
[self.superview layoutIfNeeded];
}];
Swift version of #stigi's answer which worked for me:
UIView.animate(withDuration: 0.2, animations: {
self.invalidateIntrinsicContentSize()
self.superview?.setNeedsLayout()
self.superview?.layoutIfNeeded()
})
Width / height constraint doesn't help? Keep reference of this constraint and ...
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:myViewInitialWidth];
... when you do want to animate myView resize, do this ...
self.viewWidthConstraint.constant = 100; // new width
[UIView animateWithDuration:0.3 animations:^{ [view layoutIfNeeded]; }];
... do the same thing for the height.
Depends on your other constraints, maybe you will be forced to raise priority of these two constraints.
Or you can subclass UIView, add - (void)invalidateIntrinsicContentSize:(BOOL)animated and fake it by yourself. Get new size from - (CGSize)intrinsicContentSize and animate it by animating width / height constraints. Or add property to enable / disable animations and override invalidateIntrinsicContentSize and do it inside this method. Many ways ...
In the code below, intrinsic class is the class that has just changed it's size on changing a variable. To animate the intrinsic class only use the code below. If it impacts other objects higher up the view hierarchy then replace self.intrinsic class with the top level view for setNeedsLayout and layoutIfNeeded.
[UIView animateWithDuration:.2 animations:^{
self.intrinsicClass.numberOfWeeks=8;
[self.intrinsicClass setNeedsLayout];
[self.intrinsicClass layoutIfNeeded];
}];
None of this has worked for me. I have a UILabel which I am setting with a NSAttributedString. The text is multiline and wrapping on word boundaries. Therefore the height is variable. I've tried this:
[UIView animateWithDuration:1.0f animations:^{
self.label = newLabelText;
[self.label invalidateIntrinsicContentSize];
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
}];
And a number of variations. None work. The label immediately changes it's size and then slides into it's new position. So the animation of the labels position ons screen is working. But the animating of the label's size change is not.
Well for Swift 4/3 this works and I think this is best practise. If you have a UIView with a UILabel in it and the UIView adapts the frame from the UILabel, use this:
self.theUILabel.text = "text update"
UIView.animate(withDuration: 5.0, animations: {
self.theUIView.layoutIfNeeded()
})
The normal self.view.layoutIfNeeded() will work most of the time as well.

Resizing a custom view with the window

So I've seen this question asked a couple times, but haven't come across an answer that solves my problem. Right now I basically have MainMenu.xib with a label centered on the top, a large custom view and a button centered on the bottom that switches subviews of the custom view (see picture below). I've set the window and the custom view to autoresize subviews in the interface builder and all the buttons, labels, etc. have constraints relating them to the sides of the view, but when I resize the window, the contents of the subview do not resize. I think the custom view is resizing with the window because when I switch subviews with the button, the subview that loads is resized with the window, they just aren't resizing with the window in real time.
MainMenu.xib:
Normal subview:
After window is expanded:
Subview switched and then switched back:
CONSTRAINTS:
MainMenu.xib:
BlockViewController.xib:
Figured it out! Really simple, just one line of code in the awakeFromNib method of each ViewController subclass:
[self.view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
From the pictures and the description in the comments it seems that the problem is that the custom view does not get any layout constraints to determine how to resize the subview.
The following piece of code (typed directly in the browser, so beware) should provide the necessary constraints to "glue" the borders of the subview to the custom view.
[self.customView addSubview: self.blockSubView.view];
self.blockSubView.view.frame = self.customView.bounds;
NSView *blockSubView = self.blockSubView.view;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(blockSubView);
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"|[blockSubView]|"
options:0
metrics:nil
views:viewsDictionary];
constraints = [constraints arrayByAddingObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat:#"V|[blockSubView]|"
options:0
metrics:nil
views:viewsDictionary]];
[self.customView addConstraints: constraints];
There is more about this in the docs. Notice the tricks for debugging - very useful to the point of being indispensable.