How can I trigger an update to all the autolayout constraints? - ios

I modified programmatically one autolayout constraint. It takes effect on one certain view, but other views that are bound with other constraints to that view, do not change their positions. Is there an "updateAllConstraints" method?

Call those two methods on the view you want to be updated:
-setNeedsLayout
-layoutIfNeeded
The first one says to the layout system that this view needs to be laid out, because it has some changes and, everything should be recalculated. The second force the layout system to be run now, layout system is triggered at specific times during runtime, with this method you are saying:"do it now".

Yes There is a method on UIView called - (void)updateConstraintsIfNeeded
https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/updateConstraintsIfNeeded
However I don't think this is your problem

The problem was that constrains were not set up properly, and in this case non of the setNeedsLayout or updateConstraintsIfNeeded can help.

Those of you that come across this topic, and are looking where to find this in the menus.
Best I have found is:
Select the items that you want to "pop" back to your constraints
Top menu: Editor/Update Frames (or/) shortcut: command + option + =
Hopefully this helps

Related

How to add a random view in real time when using Pure Layout

I'm using PureLayout in my iOS app and I'm following the instructions here:
https://github.com/PureLayout/PureLayout/wiki/Tips-and-Tricks
That is, using a flag called didSetupConstraints and the method updateViewConstraints.
But now I would like to add a view inside another random view when user presses a button.
The only way to do that I could imagine was creating another flag like didNewViewSetupConstraints and creating another block inside updateViewConstraints, where I use 2 UIView variables to create the new constraints between them.
Is that right? Because I think it is a lot of work for just a small thing (adding a subview and its contraints).
Note that I cannot create the constraints without installing them because I donĀ“t know the beforehand (the user will tap them). That is the reason I remark RANDOM.
I see in sample code number 10, it is used autoRemoveConstraints and autoInstallConstraints to toggle between 2 groups of constraints. Because my 2 views are random, I guess I cannot do something like that, that is the reason I created a flag and 2 variables for these 2 views to setup the constraints inside updateViewConstraints. But I'm sure that is too much.
Thanks for any suggestion or idea.
You don't have to create/activate all of your constraints inside of -updateConstraints. You can create constraints at any time, and activate them as long as both of the views you wish to constrain share a common superview (in other words, the views need to have been added to the same view hierarchy already).
I would recommend creating your default set of static constraints inside of -updateConstraints as you're doing. Then, in the callback method when the user taps the button, you can create this new view, add it as a subview, and then create & activate the new constraints for it right then and there.
If you remove this view later on, that will remove all of the constraints associated with it automatically. But if you need to adjust the constraints for this new view, you can store a separate reference to the constraints when they are created.

Storyboard connected properties not work

Im really confused because my problem may sound silly, i try to explain it clear - when i add anything on storyboard view controller it has no effect. And obvious, my viewController set properly to a controller, that manage storyboard scene. I adding anything - buttons, imageViews, it doesn't appear on a screen at all.
Also i want to add, i never seen such problem before recent Xcode update, when storyboard scene's become "large" and there is single scene for any device (600x600).
That issue literally drive me crazy, i can manage interface objects programmatically but i don't want to, because i use to work with Storyboard. Could you please write some advice or solution?
Thanks!
You need to add positioning constraints. By default, it will leave elements where they are, so they might be cut off. Also make sure what should be in the background is Sent to Back.
You can check if the auto layout this interfirindo or even if you this in the right view.

+[UIView transitionFromView:toView:...] with multiple views

So here's the situation:
I have let's say 20 views who are all subviews of the same view. Now I want to remove and add new views in an animated matter.
I previously did this by using regular UIView animations and fading them out or in respectively. The problem is though that the animation isn't flawless when the new subview who is to be inserted overlaps with the subviews who are fading out. So I tried using the +[UIView transitionFromView:toView:...] animation block to make the animation cross dissolve. This works well, animation looks good.
This basically solves my issue. The only problem is that it might be that I want to replace 2 subviews with one bigger subview. This doesn't really work out as I can't just pas nil to the transition method.
So my question basically is how I can simultaneously cross dissolve multiple subviews no matter how many views are animated?
Thanks for your help!
Have you tried putting the views to remove inside a transparent bigger view? So you just call transitionFromViewToView using the container with the 2 little ones inside and the big one that is entering?
You can use + [UIView transitionWithView:duration:options:animations:completion:], passing the common superview (usually self.view of your view controller) to perform all animations at once.
Excerpt from the doc:
This method applies a transition to the specified view so that you can
make state changes to it. The block you specify in the animations
parameter contains whatever state changes you want to make. You can
use this block to add, remove, show, or hide subviews of the specified
view. If you want to incorporate other animatable changes, you must
include the UIViewAnimationOptionAllowAnimatedContent key in the
options parameter.

iOS 7 status bar overlaps with view - did they have to make it like this?

I know this has been asked before, but none of these solutions work, and that's the reason of my posting. Please do not close before considering my case.
My plist already has UIViewControllerBasedStatusBarAppearance = false.
I have already tried applying deltas, but to no result.
Changing the top level view frame in ViewWillAppear (like self.view.frame) did not succeed.
I thought of increasing the view height (storyboard attribute inspector), in combination with deltas, but my top level view X, Y are disabled in storyboard attribute inspector.
My main view doesn't have any children views because I load them into main view either dynamically or load them from XIBs which are again shared by more than view controllers. These XIBs provide layout for both Portrait and Landscape. I don't know what approach is ideal for this kind of configuration, but I would like it better if solution lies along these lines.
This approach worked partially, but gave me inconsistent results.
What makes the solution tricky is the fact that I have to support all 4 orientations - this is something I handle in code via didRotate and willRotate delegates for my other views, but failing to do it for statusbar.
Please help...
Could this link be of any help?
You might have to use the new setEdgesForExtendedLayout: method to get this working consistently?
Also, have a look at these official docs if you haven't already done so.
I ended up writing my own function to shift my all subviews (remember, not top level views whose frame is fixated by IB).
It didn't spoil my work but imagine if this was the case for a very big project with so many screens, the limitations would have made it a nightmare.

Flipped NSScrollView problems

I have an NSScrollView that needs to display a variable amount of NSViews in it. I made a custom NSView that has isFlipped return YES and put my NSViews in that before I set it to be the NSScrollView's documentView. That works well. It displays my items top to bottom like I would expect.
But, when the contents changes and I need to change the NSScrollView's documentView's frame, my contents disappears.
This would be WAY easier with a UIScrollView, but alas.
I'm assuming these are equivalent:
[NSScrollView.documentView setFrame:newFrame];
[UIScrollView setContentSize:newSize];
I would imagine that a lot of coders need to have a flipped NSScrollView, but how do you deal with changing the content size without this madness? Obviously, I'm missing something.
Anyone? Thanks!
can you try just setting
[scrollView.documentView setFrameSize:newFrame.size];
to see if your content disappears?
The problem was with the internal views and autoresizing/constraints. My internal views are actually a series of view controllers with their own xib file. In each view controller, I added the following line:
[self.view setAutoresizingMask:NSViewNotSizable];
And that solved everything.
Special thanks to lead_the_zeppelin for helping me (in chat) go through everything.

Resources