Programatic NSLayoutConstraints in a single string - objective-c

Is there a way to combine below 3 syntax lines into a single one statement like (which doesn't work):
[_cardsContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[cardContainer1]|[cardContainer2]|[cardContainer3]|" options:0 metrics:nil views:viewsDictionary]];
1. [_cardsContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[cardContainer1]|" options:0 metrics:nil views:viewsDictionary]];
2. [_cardsContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[cardContainer2]|" options:0 metrics:nil views:viewsDictionary]];
3. [_cardsContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[cardContainer3]|" options:0 metrics:nil views:viewsDictionary]];

No, but you can do it in two lines:
[_cardsContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[cardContainer1]|" options:0 metrics:nil views:viewsDictionary]];
This pins card container 1 to the top and bottom of the superview. Then:
[_cardsContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|[cardContainer1][cardContainer2][cardContainer3]|" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:nil views:viewsDictionary]];
Which aligns the top and bottom of the remaining card containers.
This assumes you want the views laid out next to each other, you didn't actually specify any horizontal layout details.
It helps when using the visual format to think about each line of visual format language representing a single row or column of layout in your view.

To the best of my knowledge, no.

It's not really a single line, but you can reduce the duplication of your three lines by doing them in a loop:
for (NSView *view in #[cardContainer1, cardContainer2, cardContainer3]) {
[_cardsContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[view]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]];
}

Related

ConstraintsWithVisualFormat for iPhone X safe area

This line of code to be converted to safe area for iPhone X
self.view.translatesAutoresizingMaskIntoConstraints = NO;
id views = #{
#"carbonTabSwipe" : self.view,
#"topLayoutGuide" : rootViewController.topLayoutGuide,
#"bottomLayoutGuide" : rootViewController.bottomLayoutGuide
};
NSString *verticalFormat = #"V:[topLayoutGuide][carbonTabSwipe][bottomLayoutGuide]";
[rootViewController.view
addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:verticalFormat
options:0
metrics:nil
views:views]];
[rootViewController.view
addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[carbonTabSwipe]|"
options:0
metrics:nil
views:views]];
This works for other devices but iPhone X it shows space at the bottom.

UITableView and Scroll issue with UITextView

I have UITextView inside TableView and I am using UITableViewAutomaticDimension to calculate
Cell Height.
but the problem is once i start typing in UITextView the TableView Start Shaking Up & Down !
below my code in textViewDidChange event:
ImageDescriptionTableViewCell *cell = (ImageDescriptionTableViewCell *)textView.superview.superview ;
NSInteger index = [self.TableViewController indexPathForCell:cell].row;
[self.cellTextArray replaceObjectAtIndex:index withObject:textView.text];
[self.TableViewController beginUpdates];
[self.TableViewController endUpdates];
constraints in Table View Cell :
[self.ImageDetails addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:[flebImageDetails(300#999)]" options:0 metrics:nil views:#{#"ImageDetails":self.ImageDetails}]];
[self.ImageDetails addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[ImageDetails(>=30)]" options:0 metrics:nil views:#{#"ImageDetails":self.ImageDetails}]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[ImageDetails]-|" options:0 metrics:nil views:#{#"ImageDetails":self.ImageDetails}]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-5-[ImageDetails]-5-|" options:0 metrics:nil views:#{#"ImageDetails":self.ImageDetails}]];
Please help i am with issue from 1 weak ago .
I dont believe you need the beginUpdates and endUpdates in the text change event. These are only required if you are modifying (ie adding, deleting etc) multiple rows and want them animated as a single unit. Updating a text field does not require them.

How do you create a UITableView programmatically with Auto Layout?

It seems that it assumes you're using frames instead of Auto Layout, as you can only really init with a frame. Am I doing this wrong?
You can use autolayout programmatically.
You don't need to initialize the UITableView with a frame. Instead, you have to set translatesAutoresizingMaskIntoConstraints = NO, add the table view to the parent view and then define the constraints in code.
For example:
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
tableView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:tableView];
NSDictionary *views =
NSDictionaryOfVariableBindings(tableView);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:
#"H:|[tableView]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:
#"V:|[tableView]|" options:0 metrics:nil views:views]];
Please refer to https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/AdoptingAutoLayout/AdoptingAutoLayout.html to know more about the visual format. You can also choose not to use the visual format and describe the constraints programmatically.

Vertical layout constraint splitting height evenly

I am trying to build a fairly simple view.. I've got a horizontal toolbar (toolbarView) and a bottom accent line (bottomAccent) running the full width of the view. I also have two sidebars and a vertical accent next to that on the left side of the screen.
If I have one sidebar (comment out sidebarView2), life is great, it all looks just right. As soon as I add sidebarview2, I get these fun errors.. I'm not entirely sure why or what I'm doing wrong. I would expect my constraints to basically split the space for the sidebar in half, and fill each half with one of the sidebar views.. What am I missing here?
Unable to simultaneously satisfy constraints:
(
"<NSLayoutConstraint:0x7feefc3121e0 V:|-(8)-[NSTextField:0x7feefa4404e0] (Names: '|':BGView:0x7feefa440290 )>",
"<NSLayoutConstraint:0x7feefc312230 V:[NSTextField:0x7feefa4404e0]-(6)-[BGView:0x7feefa4408c0]>",
"<NSLayoutConstraint:0x7feefc3122b0 V:[BGView:0x7feefa4408c0(1)]>",
"<NSLayoutConstraint:0x7feefc312300 V:[BGView:0x7feefa4408c0]-(4)-[NSScrollView:0x7feefa440ba0]>",
"<NSLayoutConstraint:0x7feefc312350 V:[NSScrollView:0x7feefa440ba0]-(0)-| (Names: '|':BGView:0x7feefa440290 )>",
"<NSLayoutConstraint:0x7feefc313250 V:[BGView:0x7feefa440290]-(0)-[BGView:0x7feefa440290]>"
)
My code:
- (void)loadView {
NSView *rootView = [[NSView alloc] init];
NSView *toolbarView = _toolbarViewController.view;
[toolbarView setTranslatesAutoresizingMaskIntoConstraints:NO];
[rootView addSubview:toolbarView];
NSView *sidebarView = _titledScrollViewController.view;
[sidebarView setTranslatesAutoresizingMaskIntoConstraints:NO];
[rootView addSubview:sidebarView];
NSView *sidebarView2 = _titledScrollViewController.view;
[sidebarView2 setTranslatesAutoresizingMaskIntoConstraints:NO];
[rootView addSubview:sidebarView2];
BGView *horizontalSpacerSidebar = [[BGView alloc] initWithBackgroundColor:[NSColor colorWithDeviceRed:0.391 green:0.391 blue:0.43 alpha:1.0]];
[horizontalSpacerSidebar setTranslatesAutoresizingMaskIntoConstraints:NO];
[rootView addSubview:horizontalSpacerSidebar];
BGView *bottomAccent = [[BGView alloc] initWithBackgroundColor:[NSColor colorWithDeviceRed:0.223 green:0.77 blue:0.93 alpha:1.0]];
[bottomAccent setTranslatesAutoresizingMaskIntoConstraints:NO];
[rootView addSubview:bottomAccent];
NSDictionary *views = NSDictionaryOfVariableBindings(toolbarView, bottomAccent, sidebarView, sidebarView2, horizontalSpacerSidebar);
[rootView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[toolbarView]|" options:0 metrics:nil views:views]];
[rootView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[sidebarView(==200)][horizontalSpacerSidebar(==1)]" options:0 metrics:nil views:views]];
[rootView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[sidebarView2(==200)][horizontalSpacerSidebar(==1)]" options:0 metrics:nil views:views]];
[rootView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[bottomAccent]|" options:0 metrics:nil views:views]];
[rootView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[toolbarView(==53)][sidebarView][sidebarView2][bottomAccent(==3)]|" options:0 metrics:nil views:views]];
[rootView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[toolbarView(==53)][horizontalSpacerSidebar][bottomAccent(==3)]|" options:0 metrics:nil views:views]];
self.view = rootView;
}

Aligning NSViews Using Autolayout Programmatically

I am having troubles aligning NSViews next to each other using Autolayout programmatically. To illustrate, I have made a very simple example project. DiscView is a subclass of NSView which draws a disc with a specified color.
In the example below, I only add Autolayout constraints on one of two subviews, and the result is as I expected (see screenshot below).
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
DiscView *discViewA = [[DiscView alloc] init];
DiscView *discViewB = [[DiscView alloc] init];
discViewA.color = [NSColor blackColor];
discViewB.color = [NSColor redColor];
discViewA.translatesAutoresizingMaskIntoConstraints = NO;
discViewB.translatesAutoresizingMaskIntoConstraints = NO;
[self.customView addSubview:discViewA];
[self.customView addSubview:discViewB];
NSDictionary *discViewDictionary = #{#"discViewA" : discViewA,
#"discViewB" : discViewB};
[self.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[discViewA]|" options:0
metrics:nil
views:discViewDictionary]];
[self.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[discViewA]|" options:0
metrics:nil
views:discViewDictionary]];
}
However, if I try to show the two subviews side by side using some additional constraints, I only see a red disc. That is, it is as if the red disc is drawn directly on top of the black disc.
[self.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[discViewA]-[discViewB]|" options:0
metrics:nil
views:discViewDictionary]];
[self.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[discViewA]|" options:0
metrics:nil
views:discViewDictionary]];
[self.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[discViewB]|" options:0
metrics:nil
views:discViewDictionary]];
What could possibly be wrong here?
[self.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[discViewA][discViewB]|"
options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom
views:discViewDictionary]];
[self.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[discViewA]|" options:0
metrics:nil
views:discViewDictionary]];
Try this. It might help. If not do what someone else suggest and put a constraint like so.
[self.customView addConstraint:[NSLayoutConstraint constraintWithItem:discViewA
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.customView
attribute:NSLayoutAttributeWidth
multiplier:0.5f
constant:0.f]];
My guess is that when you set both view to be side by side, AutoLayout does not know how to divide the width of the superview between your two views.
Try to add a width constraint to one of those subviews, it should be sufficient to resolve the ambiguity.