Autolayout NSScrollview does not grow contentView - objective-c

I have a scrollview that I'm trying to setup with auto layout.
The actual content is in a container view, this container view a set of visible box views. They are added without any problem to the container view. Each one is given a couple of constraints
NSViewController prev;
for(NSViewController *lvc in vcs) {
//each view is padded by 5 from left
[containerView addConstraint:[NSLayoutConstraint constraintWithItem:lvc.view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:prev.view attribute:NSLayoutAttributeTrailing multiplier:1 constant:5]];
//Constant height
[containerView addConstraint:[NSLayoutConstraint constraintWithItem:lvc.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:300]];
//Constant width
[containerView addConstraint:[NSLayoutConstraint constraintWithItem:lvc.view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:300]];
prev = lvc;
}
[_scrollView setDocumentView:containerView];
[_scrollView addConstraint:[NSLayoutConstraint constraintWithItem:listContainerView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:_scrollView attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
However the scroll view does not allow the container view to grow without growing the scrollview itself. The right edge of the containerView is attached to the right edge of the scroll view. If I grow the scroll view I can see there's more content from the list container view but only as big as the scroll view is.
The last row is my attempt to fix this by basically saying "let the container view grow larger than the scroll view". Isn't that the correct way of setting the constraint?

Add an NSStackView to your NIB and then select Editor > Embed In > Scroll View. Add constraints to the scroll view to keep it laid out relative to its superview and/or sibling views. Add constraints between the stack view and its superview (the clip view) to keep its top and leading edges the same as the clip view's. Add a constraint to keep the stack view's trailing edge greater than or equal to the clip view's trailing edge. Add a constraint to the keep the stack view's bottom edge greater than or equal to the clip view's bottom edge.
In your code, add the views of the view controllers in vcs as subviews of the stack view. The stack view's spacing will default to 8. In your code, you set spacing of 5. So, you can change the stack view's spacing to 5. (This can be done in the NIB.)

Related

Using Xib's for a multi-device UI

I am using a xib file to create the view for a settings view controller. The user taps an object in the main view which animates a narrow view (preferably 35% the width of the screen) over from the left.
How do I set a relative width using xibs. Right now I am setting the simulated metrics - > size property to freeform and I only have the option to hard code a width in points.
Autolayout - make a width constraint for the view:
myView.translatesAutoresizingMaskIntoConstraints = NO;
[myView addConstraint: [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:.35]];

UIScrollView breaks when using autolayout for size on iOS 8 / Xcode 6 GM Seed

How do I get a Scroll View to work using autolayout to calculate its content size?
It seems Xcode 6 will create a "UIView Encapsulated Layout Height" constraint whenever a Scroll View (with Autolayout) exists, and this constraint will forcibly set the height to the frame height (thus rendering the scroll functionality of a scroll view useless).
Error:
2014-09-09 21:06:01.059 ScrollViewLayoutBreaking[24488:88731] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7f9a19ebd150 V:[UIView:0x7f9a19ea7f90(530)]>",
"<NSLayoutConstraint:0x7f9a19ec1370 V:[UIView:0x7f9a19ebd5e0(110)]>",
"<NSLayoutConstraint:0x7f9a19ea9ee0 V:[_UILayoutGuide:0x7f9a19ec18d0]-(0)-[UIView:0x7f9a19ea7f90]>",
"<NSLayoutConstraint:0x7f9a19ec1fa0 V:[UIView:0x7f9a19ea7f90]-(0)-[UIView:0x7f9a19ebd5e0]>",
"<NSLayoutConstraint:0x7f9a19e8d270 V:[UIView:0x7f9a19ebd5e0]-(5)-[_UILayoutGuide:0x7f9a19ec2260]>",
"<_UILayoutSupportConstraint:0x7f9a19ea0ac0 V:[_UILayoutGuide:0x7f9a19ec18d0(0)]>",
"<_UILayoutSupportConstraint:0x7f9a19ea7380 V:|-(0)-[_UILayoutGuide:0x7f9a19ec18d0] (Names: '|':UIScrollView:0x7f9a19ea76a0 )>",
"<_UILayoutSupportConstraint:0x7f9a19e21d00 V:[_UILayoutGuide:0x7f9a19ec2260(0)]>",
"<_UILayoutSupportConstraint:0x7f9a19e8a930 _UILayoutGuide:0x7f9a19ec2260.bottom == UIScrollView:0x7f9a19ea76a0.bottom>",
"<NSLayoutConstraint:0x7f9a1c003f50 'UIView-Encapsulated-Layout-Height' V:[UIScrollView:0x7f9a19ea76a0(504)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7f9a19ebd150 V:[UIView:0x7f9a19ea7f90(530)]>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
Edit: All views inside this Scroll View are autolayouted and already pinned to both the Scroll View top and bottom, as well as both sides. I am aware that failing to bind it in all four sides will fail to create a content size. The autolayout and scrolling are correct in iOS 7 / XCode 5, but the exact same code and storyboard break in iOS 8 / XCode 6.
By your Screenshot, I'm guessing that you are using custom views in a scrollview so...
Your Content Views Are : STH,Foo,Bar,Baz,Qux,Blah,Woot,Lorem,Ipsum, Dolor
In code you can set the content size automatically with autolayout by setting all your content views related by each other, with the exception of the top and bottom view... these ones must be related to the edges of the scrollview, Here is an Example:
-(void)example{
UIView *parentView; //Can be the Scroll View or the Scroll View superview, it's better for the ScrollView to be the Owner of the restrictions
UIView *previousView;
UIScrollView *scrollView;
NSArray * arrayOfContentViews = #[STH,Foo,Bar,Baz,Qux,Blah,Woot,Lorem,Ipsum,Dolor];
for (UIView * currentView in arrayOfContentViews) {
//Important for Autolayout Restrictions
currentView.translateAutoresizingMask = NO;
[scrollview addSubview:currentView];
if (previousView == nil) {
//For the Top, Left and Right Attribute Edges of the ScrollView
previousView = scrollView;
[parentView addConstraint:[NSLayoutConstraint constraintWithItem:currentView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:previousView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0]];
//10% of the scrollview Frame Size (not Content Size)
[parentView addConstraint:[NSLayoutConstraint constraintWithItem:currentView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:previousView
attribute:NSLayoutAttributeHeight
multiplier:0.1
constant:0.0]];
}else{
[parentView addConstraint:[NSLayoutConstraint constraintWithItem:currentView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:previousView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0]];
[parentView addConstraint:[NSLayoutConstraint constraintWithItem:currentView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:previousView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0.0]];
}
[parentView addConstraint:[NSLayoutConstraint constraintWithItem:currentView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:previousView
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:0.0]];
[parentView addConstraint:[NSLayoutConstraint constraintWithItem:currentView
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:previousView
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:0.0]];
previousView = currentView;
}
//Last View Dolor to the Bottom Edge of the scrollView
[parentView addConstraint:[NSLayoutConstraint constraintWithItem:previousView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:scrollView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0]];
}
If you are using IB, then select the UIViewController and select remove all constraints from the AutoLayout menu at the bottom. Then select add missing constraints. This will solve your problem
For UIScrollView to work vertically it needs the chain of subviews from top to bottom.
Now if you set the top/bottom margin of its subviews you left no idea to scroll view how it should render it self vertically as it calculates its own height based on its content
So what you should do is allow scrollview to determine its own height independent of its children.
So add your scrollview in another view as subview with constraint related to that view and then you are free to add children relative to scrollview.
Figured I'd answer the question since it's receiving replies up to this day.
The code is autolayouted correctly. As stated in the question, the same code produced a perfect, no warning/errors Scroll View using Xcode 5 and iOS 7, and would break a layout constraint I did not manually create in Xcode 6 GM Seed and iOS 8.
It so seems it was an Xcode 6 GM Seed bug, the same code actually worked seamlessly in future updates of Xcode 6 after the first release, without any constraint breaking.

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:.

Subview disappears with auto layout

I just added a UIView as subview to the main UIView on interface builder (basic single view application).
Without setting any constraints, my subview disappears.
subview's frame = (0 0; 320 0);
Why is that?
If I try to add some constraints like for trailing space, leading space, top space and bottom space to be fixed, still my view disappears.
How can I solve this?
Thank you.
Just to clarify thing a little I created a test project (single view application), and added 2 subview to the main view like in the image. I didn't change any default constraint.
And you can see the error in the log of the image.
Logs:
**2013-01-19 17:16:02.435 Test[8871:c07] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x106178c0 h=--- v=--- V:[UIWindow:0x9917040(480)]>",
"<NSLayoutConstraint:0x106159e0 UIView:0x991a5a0.bottom == UIWindow:0x9917040.bottom>",
"<NSLayoutConstraint:0x991ab00 V:|-(518)-[BottomView:0x9919c90] (Names: '|':UIView:0x991a5a0 )>",
"<NSLayoutConstraint:0x10615960 V:|-(20)-[UIView:0x991a5a0] (Names: '|':UIWindow:0x9917040 )>",
"<NSLayoutConstraint:0x991aa80 BottomView:0x9919c90.bottom == UIView:0x991a5a0.bottom>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x991aa80 BottomView:0x9919c90.bottom == UIView:0x991a5a0.bottom>
Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.**
Constraints:
Also here is the result on the simulator:
It is good practice to understand those logs but if you are going to use Autolayout you are going to have to read up on this. Everyone says it is simply but I personally have not found it simple.
Apples Programming Guide For AutoLayout
Please read this guide especially the debugging section.
As a very very general rule if you are going to add a view then you need to turn off autoresizingmasks (Spring and struts) for the view. Add the view as a subview and give it 2 or 3 constraints. In your case above you would give it a constaint that it should have a left or leading space to superview of 0. A top space to superview of 0 and a width of 320.
EDIT; Here is an example of adding a view; note you do not need to create a frame. The constraints may be a little strange. The first puts the view in the centre of the superview. The second gives it a width of 200. The next method is the vertical constraint which puts the view at the bottom and makes it 2 high.
UIView *sView = [[UIView alloc] init];
[sView setTranslatesAutoresizingMaskIntoConstraints:NO];
[superView addSubview:sView];
[superView addConstraint:[NSLayoutConstraint constraintWithItem:sView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:superView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0]];
[superView addConstraint:[NSLayoutConstraint constraintWithItem:sView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:Nil
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:200]];
[superView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"V:[sView(2)]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(sView)]];