iOS 8: UITableViewCell.contentView doesn't resize subviews in it - ios

I put an CustomView in UITableViewCell.contentView, setting the autoresize mask of the custom view as W+H.
But when running, the CustomView gets a larger height than the ContentView, the ContentView is 60pt in height(which is the same as setting in UITableViewDelegate), but the CustomView inside contentView is 76pt in height.
By Xcode6's view debugging, I see some strange constraints on my custom view, they are:
self.height = superview.height + 16
self.midY = superview.midY + 8
Where are these constraints come from and how to modify them? I've never set anything with a value 8 or 16.
UPDATE:
I've made a test project, which is simply a tableview in storyboard with a CustomView loaded from a nib file, and this test project replicated the problem, when running, the CustomView which is a subview of TableViewCell.contentView becomes larger in height than the TableViewCell.contentView.
The test project is here:
https://drive.google.com/file/d/0B5y_NrRbhGlSb1dlbVZNb19vNjQ/view?usp=sharing

At last I understand, auto layout can only form relations in the same xib or storyboard, my CustomView is in a separated xib and is loaded at runtime, so the super view and CustomView don't have any auto layout constraints between them.
If I set translatesAutoresizingMaskIntoConstraints to YES, then there goes the problem, I still don't know why using W+H as auto resizing mask makes CustomView taller than its super view(cell.contentView), but I found a way around:
I manually add constraints between superview and CustomView, and turn off customView. translatesAutoresizingMaskIntoConstraints, there the code goes:
customView.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:customView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:customView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:customView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:customView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeRight multiplier:1 constant:0];
[cell.contentView addConstraints:#[top, bottom, left, right]];

Related

Programmatically add constraint to view relative to a sibling

I am using Auto Layout. I have a view with a subview (sb1). I am programmatically adding another subview (sb2) to this view. I am adding constraints to this new subview.
What I want accomplish:
add sb2 below sb1 an add a constraint to the top of sb2 to pin it to sb1.
What I've tried:
[self.containerView insertSubview:sb1
belowSubview:sb2];
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:sb1
attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual
toItem:sb2 attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.f];
[self.otherLeftView addConstraint:constraint];
What is happening is my newly added subview is being pinned to the top of the container view, NOT to the sb1 (which is what I want to happen). Visual of what is happening:
Does anyone know how I can add a constraint between SB1 and SB2? Thanks!
Try this:
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:sb1
attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual
toItem:sb2 attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.f];

Constraints for subview not working in iOS 8, XCode 6 (6A313). Works iOS 7

Simply adding a subview UIView from a controller creating using instantiateViewControllerWithIdentifier to the current controllers view and adding constraints to maintain the size of the new subview to cover the entire area. The code below worked in iOS 7. iOS 8 sizes the subview to a small portion of the upper left corner. iOS 7 does what I expect, which is to size the subview across the entire size of the parent view. I'd attach an image, but don't have the rep for that. Setting translatesAutoresizingMaskIntoConstraints to YES fixes the issue, but then the view does not honor the constraints and resize when orientation changes or sizing changes.
spotCheckStoresViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"visitStoresViewController"];
spotCheckStoresViewController.mainViewController = self;
spotCheckStoresViewController.dataMode = kDataModeViews;
spotCheckStoresViewController.lastRow = [self getViewsLastRow];
spotCheckStoresViewController.view.tag = -200;
currentView = spotCheckStoresViewController.view;
[self addChildViewController:spotCheckStoresViewController];
[self.view insertSubview:currentView belowSubview:_menuViewController.view];
currentView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:currentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:currentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:currentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:currentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
//[self.view updateConstraints];
//[self.view layoutIfNeeded];
I've tried setting the frame of the subview as well. Any ideas what might have changed in iOS 8 to alter the behavior of constraints used in this way?
In my case, the problem related to constraints labeled UIView-Encapsulated-Layout-Width and UIView-Encapsulated-Layout-Height. When I removed them, everything behaved as though my view was of zero size, with everything centered on the upper left corner of the screen. When I left them in, new constraints worked as expected. I also retained the constraints labeled _UILayoutSupportConstraint.
In my case, it was only happening on iOS 8.3. There was a conflict with existing constraints. A colleague found that I needed to first remove any existing constraints before adding the others.
[self.view removeConstraints:self.constraints];

Constraint added programmatically to a xib view not working

I have a view(self.printSettingsView) created from xib. I add this view as a subview to another view(self.view). I programmatically add constraints as follows:
[self.printSettingView setTranslatesAutoresizingMaskIntoConstraints: NO];
NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:self.printSettingView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0];
[self.view addConstraint:leftConstraint];
NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:self.printSettingView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.topBar
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0];
[self.view addConstraint:topConstraint];
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self.printSettingView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0];
[self.view addConstraint:heightConstraint];
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:self.printSettingView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0];
[self.view addConstraint:widthConstraint];
All the other constraints take effect except for height.
What could i be doing wrong here???
Thanks
Without knowing the constraints on your xib file or what you expect to happen vs what is happening (a screenshot would be helpful) it's hard to say. However I do have one suggestion where maybe the logic isn't correct.
The second constraint is pinning printViewSettings top to the bottom of the topBar, that part makes sense. The next one however sets the height of printViewSettings to the height of its superview. This may not jive with what you want because your superview contains your topBar as well and so may be larger than you expect. What you might actually want is a constraint that pins the bottom of printViewSettings to the bottom of the superview instead.
Sorry guys about the incomplete information. The problem in a gist was that i was assigning a constraint to the subview in my main view and whatever be the constant of the constraint the size of the subview remained constant. I found that the issue was the subview in turn had components(subviews) with fixed height constraints. I made them proportional to the height of the parent view and it works now.

Why am I unable to set Auto Layout constraints when adding constraints to a view I added to UIWindow?

To create a dark overlay on the screen, I add a view over all the other views onto UIWindow:
UIView *darkOverlayView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
darkOverlayView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.85];
I then want to center a UIImageView in the new darkOverlayView, and try to as follows:
UIImageView *imageFromLink = [[UIImageView alloc] initWithImage:responseObject];
imageFromLink.translatesAutoresizingMaskIntoConstraints = NO;
CGFloat widerThanHeightBy = imageFromLink.bounds.size.width / imageFromLink.bounds.size.height;
[darkOverlayView addConstraint:[NSLayoutConstraint constraintWithItem:imageFromLink attribute:NSLayoutAttributeWidth relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:[UIScreen mainScreen].bounds.size.width]];
[darkOverlayView addConstraint:[NSLayoutConstraint constraintWithItem:imageFromLink attribute:NSLayoutAttributeHeight relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:[UIScreen mainScreen].bounds.size.height / widerThanHeightBy]];
But every time it runs, I get the error:
The view hierarchy is not prepared for the constraint:
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2013-12-07 00:59:03.626 Jupiter[58008:70b] View hierarchy unprepared for constraint.
Constraint:
Container hierarchy:
>
View not found in container hierarchy: > - (null)
That view's superview: NO SUPERVIEW
How would I get around this? Or do I have to use frames?
How say #rdelmar:
First: Where you add your imageFromLink like addSubView on to superview?
Seccond:
Your constraints:
[darkOverlayView addConstraint:[NSLayoutConstraint constraintWithItem:imageFromLink attribute:NSLayoutAttributeWidth relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:[UIScreen mainScreen].bounds.size.width]];
[darkOverlayView addConstraint:[NSLayoutConstraint constraintWithItem:imageFromLink attribute:NSLayoutAttributeHeight relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:[UIScreen mainScreen].bounds.size.height / widerThanHeightBy]];
setup only Width and height, what about x and y
just add two constraints more:
[darkOverlayView addConstraint:[NSLayoutConstraint constraintWithItem:imageFromLink attribute:NSLayoutAttributeCenterX relatedBy:0 toItem:darkOverlayView attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];
[darkOverlayView addConstraint:[NSLayoutConstraint constraintWithItem:imageFromLink attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:darkOverlayView attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f]];
it's set position in center of darkOverlayView.
When adding constraints to view that involves two views, the one view has to be a subview of the other, so you would need to add imageFromLink as a subview of darkOverLayView before you add the constraints. However, in this case, where you're adding a width and height constraint, those constraints should be added to imageFromLink, not to its superview. This type of fixed width or height constraint doesn't involve any other views, so it should belong to the view itself, not the superview.
Adding a height and width constraint doesn't center it in its superview however. You need to add (after making imageFromLink a subview) a centerX and centerY constraint to darkOverlayView as well.

iOS: Autolayout: Lock new position after animating

I tried to write a category function for UIView that will get the constraints that will hold an given frame in place in its superview. I implemented it like so:
-(NSArray *)constraintsForLockingPositionInSuperview
{
NSLayoutConstraint *left=[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.superview
attribute:NSLayoutAttributeLeft multiplier:0 constant:self.frame.origin.x];
NSLayoutConstraint *height=[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:Nil
attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:self.frame.size.height];
NSLayoutConstraint *width=[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:Nil
attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:self.frame.size.width];
NSLayoutConstraint *top=[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.superview
attribute:NSLayoutAttributeTop multiplier:0 constant:self.frame.origin.y];
return #[left,height,width,top ];
}
And then after animating the view, apply to new constraints by doing the following in the view controller that contains the view in question. I remove the view then re-add it to remove the constraints that were on it, then reapply the new constraints. The hope was that this would hold the view in place if I add another subview or if something like an actionView comes in and the view has to layout itself out. The height and the width seem to be locking properly, but the view is jumping to the middle instead of locking in place:
NSArray *lockingConstraints = [someView constraintsForLockingPositionInSuperview];
[someView removeFromSuperview];
[self.view addSubview:someView;
[self.view addConstraints:lockingConstraints];
[self.view layoutSubviews];
From the documentation for layoutSubviews:
You should not call this method directly.
Call layoutIfNeeded instead.

Resources