Paging UIScrollView with autolayout programmatically? - uiscrollview

I'm trying to copy reproduce the answer to this question: UIScrollView Paging Autolayout & Storyboard
programmatically instead of using interface builder.
I'm getting some constraint conflicts and I cant figure out why?
- (void)viewDidLoad {
[super viewDidLoad];
// self.view is always the size of the application window
self.view = [[UIView alloc] init];
self.view.backgroundColor = [UIColor blackColor];
self.dummyView = [[UIView alloc] init];
self.dummyView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.dummyView];
self.scrollView = [[UIScrollView alloc] init];
self.scrollView.backgroundColor = [UIColor blueColor];
[self.dummyView addSubview:self.scrollView];
self.contentView = [[UIView alloc] init];
self.contentView.backgroundColor = [UIColor orangeColor];
[self.scrollView addSubview:self.contentView];
self.pageView = [[UIView alloc] init];
self.pageView.backgroundColor = [UIColor yellowColor];
[self.contentView addSubview:self.pageView];
self.pageView2 = [[UIView alloc] init];
self.pageView2.backgroundColor = [UIColor greenColor];
[self.contentView addSubview:self.pageView2];
self.pageView3 = [[UIView alloc] init];
self.pageView3.backgroundColor = [UIColor purpleColor];
[self.contentView addSubview:self.pageView3];
// constraints must be added after the view heirarchy is established
[self addConstraints];
}
- (void) addConstraints {
// Tell autoLayout to use my constraints (as opposed to inventing its own)
self.view.translatesAutoresizingMaskIntoConstraints = NO;
// **** Dummy View constraints:
// dummyView.leading = view.leading
NSLayoutConstraint* myConstraint = [NSLayoutConstraint
constraintWithItem:self.dummyView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0];
myConstraint.identifier = #"dummyView.leading = view.leading";
[self.view addConstraint:myConstraint];
// dummyView.trailing = view.trailing
myConstraint =[NSLayoutConstraint
constraintWithItem:self.dummyView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0];
myConstraint.identifier = #"dummyView.trailing = view.trailing";
[self.view addConstraint:myConstraint];
// dummyView.top = view.top
myConstraint =[NSLayoutConstraint
constraintWithItem:self.dummyView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0];
myConstraint.identifier = #"dummyView.top = view.top";
[self.view addConstraint:myConstraint];
// dummyView.bottom = view.bottom
myConstraint =[NSLayoutConstraint
constraintWithItem:self.dummyView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0];
myConstraint.identifier = #"dummyView.bottom = view.bottom";
[self.view addConstraint:myConstraint];
// **** Scroll View constraints
// scrollView.trailing = dummyView.trailing
myConstraint =[NSLayoutConstraint
constraintWithItem:self.scrollView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.dummyView
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0];
myConstraint.identifier = #"scrollView.trailing = dummyView.trailing";
[self.dummyView addConstraint:myConstraint];
// scrollView.leading = dummyView.leading
myConstraint =[NSLayoutConstraint
constraintWithItem:self.scrollView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.dummyView
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0];
myConstraint.identifier = #"scrollView.leading = dummyView.leading";
[self.dummyView addConstraint:myConstraint];
// scrollView.Top = dummyView.top
myConstraint =[NSLayoutConstraint
constraintWithItem:self.scrollView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.dummyView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0];
myConstraint.identifier = #"scrollView.Top = dummyView.top";
[self.dummyView addConstraint:myConstraint];
// scrollView.bottom = dummyView.bottom
myConstraint =[NSLayoutConstraint
constraintWithItem:self.scrollView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.dummyView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0];
myConstraint.identifier = #"scrollView.bottom = dummyView.bottom";
[self.dummyView addConstraint:myConstraint];
// **** Content View Constraints
// contentView.width = dummyView.width * 3
myConstraint =[NSLayoutConstraint
constraintWithItem:self.contentView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.dummyView
attribute:NSLayoutAttributeWidth
multiplier:3.0
constant:0];
myConstraint.identifier = #"contentView.width = dummyView.width * 3";
[self.dummyView addConstraint:myConstraint];
// contentView.height = dummyView.height
myConstraint =[NSLayoutConstraint
constraintWithItem:self.contentView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.dummyView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0];
myConstraint.identifier = #"contentView.height = dummyView.height";
[self.dummyView addConstraint:myConstraint];
// contentView.leading = scrollView.leading
myConstraint =[NSLayoutConstraint
constraintWithItem:self.contentView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0];
myConstraint.identifier = #"contentView.leading = scrollView.leading";
[self.scrollView addConstraint:myConstraint];
// contentView.trailing = scrollView.trailing
myConstraint =[NSLayoutConstraint
constraintWithItem:self.contentView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0];
myConstraint.identifier = #"contentView.trailing = scrollView.trailing";
[self.scrollView addConstraint:myConstraint];
// contentView.Top = scrollView.top
myConstraint =[NSLayoutConstraint
constraintWithItem:self.contentView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0];
myConstraint.identifier = #"contentView.Top = scrollView.top";
[self.scrollView addConstraint:myConstraint];
// contentView.bottom = scrollView.bottom
myConstraint =[NSLayoutConstraint
constraintWithItem:self.contentView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0];
myConstraint.identifier = #"contentView.bottom = scrollView.bottom";
[self.scrollView addConstraint:myConstraint];
// **** Page View 1 Constraints
// pageView.leading = contentView.leading
myConstraint =[NSLayoutConstraint
constraintWithItem:self.pageView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0];
myConstraint.identifier = #"pageView.leading = contentView.leading";
[self.contentView addConstraint:myConstraint];
// pageView.top = contenentView.top
myConstraint =[NSLayoutConstraint
constraintWithItem:self.pageView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0];
myConstraint.identifier = #"pageView.top = contenentView.top";
[self.contentView addConstraint:myConstraint];
// pageView.width = dummyView.width
myConstraint =[NSLayoutConstraint
constraintWithItem:self.pageView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.dummyView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0];
myConstraint.identifier = #"pageView.width = dummyView.width";
[self.dummyView addConstraint:myConstraint];
// pageView.height = dummyView.height
myConstraint =[NSLayoutConstraint
constraintWithItem:self.pageView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.dummyView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0];
myConstraint.identifier = #"pageView.height = dummyView.height";
[self.dummyView addConstraint:myConstraint];
// **** Page View 2 Constraints
// pageView2.height = pageView.height
myConstraint =[NSLayoutConstraint
constraintWithItem:self.pageView2
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.pageView2
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0];
myConstraint.identifier = #"pageView2.height = pageView.height";
[self.contentView addConstraint:myConstraint];
// pageView2.width = pageView.width
myConstraint =[NSLayoutConstraint
constraintWithItem:self.pageView2
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.pageView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0];
myConstraint.identifier = #" pageView2.width = pageView.width";
[self.contentView addConstraint:myConstraint];
// pageView2.top = pageView.top
myConstraint =[NSLayoutConstraint
constraintWithItem:self.pageView2
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.pageView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0];
myConstraint.identifier = #"pageView2.top = pageView.top";
[self.contentView addConstraint:myConstraint];
// pageView2.leading = pageView.trailing
myConstraint =[NSLayoutConstraint
constraintWithItem:self.pageView2
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.pageView
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0];
myConstraint.identifier = #"pageView2.leading = pageView.trailing";
[self.contentView addConstraint:myConstraint];
// **** Page View 3 Constraints
// pageView3.height = pageView.height
myConstraint =[NSLayoutConstraint
constraintWithItem:self.pageView3
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.pageView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0];
myConstraint.identifier = #"pageView3.height = pageView.height";
[self.contentView addConstraint:myConstraint];
// pageView3.width = pageView.width
myConstraint =[NSLayoutConstraint
constraintWithItem:self.pageView3
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.pageView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0];
myConstraint.identifier = #"pageView3.width = pageView.width";
[self.contentView addConstraint:myConstraint];
// pageView3.Top = pageView.Top
myConstraint =[NSLayoutConstraint
constraintWithItem:self.pageView3
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.pageView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0];
myConstraint.identifier = #"pageView3.Top = pageView.Top";
[self.contentView addConstraint:myConstraint];
// pageView3.leading = pageView2.trailing
myConstraint =[NSLayoutConstraint
constraintWithItem:self.pageView3
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.pageView2
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0];
myConstraint.identifier = #"pageView3.leading = pageView2.trailing";
[self.contentView addConstraint:myConstraint];
The errors are:
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:0x134e1c340 h=--& v=--& UIView:0x134e3e740.midX ==>",
"<NSAutoresizingMaskLayoutConstraint:0x134d47a20 h=--- v=--- H:[UIWindow:0x134e3a6c0(375)]>",
"<NSLayoutConstraint:0x134e4c960 H:|-(0)-[UIView:0x134e3bdf0](LTR) (Names: '|':UIWindow:0x134e3a6c0 )>",
"<NSLayoutConstraint:0x134e4ca00 UIView:0x134e3bdf0.right == UIWindow:0x134e3a6c0.right>",
"<NSLayoutConstraint:0x134e3f150 'dummyView.leading = view.leading' H:|-(0)-[UIView:0x134e3cb00] (Names: '|':UIView:0x134e3bdf0 )>",
"<NSLayoutConstraint:0x134e3f480 'dummyView.trailing = view.trailing' UIView:0x134e3cb00.trailing == UIView:0x134e3bdf0.trailing>",
"<NSLayoutConstraint:0x134e3fc60 'pageView.leading = contentView.leading' H:|-(0)-[UIView:0x134e3e740] (Names: '|':UIView:0x134e3e450 )>",
"<NSLayoutConstraint:0x134e3fd30 'pageView.width = dummyView.width' UIView:0x134e3e740.width == UIView:0x134e3cb00.width>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x134e3fc60 'pageView.leading = contentView.leading' H:|-(0)-[UIView:0x134e3e740] (Names: '|':UIView:0x134e3e450 )>
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.
2016-05-26 10:28:55.062 autolayouttest[682:208278] 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:0x134d3e930 h=--& v=--& UIView:0x134e3e740.midY ==>",
"<NSAutoresizingMaskLayoutConstraint:0x134d45af0 h=--- v=--- V:[UIWindow:0x134e3a6c0(667)]>",
"<NSLayoutConstraint:0x134e4c910 V:|-(0)-[UIView:0x134e3bdf0] (Names: '|':UIWindow:0x134e3a6c0 )>",
"<NSLayoutConstraint:0x134e4c9b0 UIView:0x134e3bdf0.bottom == UIWindow:0x134e3a6c0.bottom>",
"<NSLayoutConstraint:0x134e3f540 'dummyView.bottom = view.bottom' UIView:0x134e3cb00.bottom == UIView:0x134e3bdf0.bottom>",
"<NSLayoutConstraint:0x134e3f4d0 'dummyView.top = view.top' V:|-(0)-[UIView:0x134e3cb00] (Names: '|':UIView:0x134e3bdf0 )>",
"<NSLayoutConstraint:0x134e3f9c0 'pageView.height = dummyView.height' UIView:0x134e3e740.height == UIView:0x134e3cb00.height>",
"<NSLayoutConstraint:0x134e3fce0 'pageView.top = contenentView.top' V:|-(0)-[UIView:0x134e3e740] (Names: '|':UIView:0x134e3e450 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x134e3fce0 'pageView.top = contenentView.top' V:|-(0)-[UIView:0x134e3e740] (Names: '|':UIView:0x134e3e450 )>
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.
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:0x134d47a20 h=--- v=--- H:[UIWindow:0x134e3a6c0(375)]>",
"<NSAutoresizingMaskLayoutConstraint:0x134d476e0 h=--& v=--& UIScrollView:0x135000000.midX ==>",
"<NSLayoutConstraint:0x134e4c960 H:|-(0)-[UIView:0x134e3bdf0](LTR) (Names: '|':UIWindow:0x134e3a6c0 )>",
"<NSLayoutConstraint:0x134e4ca00 UIView:0x134e3bdf0.right == UIWindow:0x134e3a6c0.right>",
"<NSLayoutConstraint:0x134e3f150 'dummyView.leading = view.leading' H:|-(0)>",
"<NSLayoutConstraint:0x134e3f480 'dummyView.trailing = view.trailing' UIView:0x134e3cb00.trailing == UIView:0x134e3bdf0.trailing>",
"<NSLayoutConstraint:0x134e3f610 'scrollView.leading = dummyView.leading' H:|-(0)-[UIScrollView:0x135000000] (Names: '|':UIView:0x134e3cb00 )>",
"<NSLayoutConstraint:0x134e3f590 'scrollView.trailing = dummyView.trailing' UIScrollView:0x135000000.trailing == UIView:0x134e3cb00.trailing>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x134e3f590 'scrollView.trailing = dummyView.trailing' UIScrollView:0x135000000.trailing == UIView:0x134e3cb00.trailing>
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.
2016-05-26 10:28:55.067 autolayouttest[682:208278] 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:0x134d47a20 h=--- v=--- H:[UIWindow:0x134e3a6c0(375)]>",
"<NSAutoresizingMaskLayoutConstraint:0x134d43660 h=--& v=--& UIView:0x134e3cb00.midX ==>",
"<NSLayoutConstraint:0x134e4c960 H:|-(0)-[UIView:0x134e3bdf0](LTR) (Names: '|':UIWindow:0x134e3a6c0 )>",
"<NSLayoutConstraint:0x134e4ca00 UIView:0x134e3bdf0.right == UIWindow:0x134e3a6c0.right>",
"<NSLayoutConstraint:0x134e3f150 'dummyView.leading = view.leading' H:|-(0)-[UIView:0x134e3cb00] (Names: '|':UIView:0x134e3bdf0 )>",
"<NSLayoutConstraint:0x134e3f480 'dummyView.trailing = view.trailing' UIView:0x134e3cb00.trailing == UIView:0x134e3bdf0.trailing>"
Any help would be appriciated. Thank you!

// Tell autoLayout to use my constraints (as opposed to inventing its
own)
self.view.translatesAutoresizingMaskIntoConstraints = NO;
This has to be done for all view created programatically. Since you have not created self.view you should not change translatesAutoresizingMaskIntoConstraints for this view.
// Tell autoLayout to use my constraints (as opposed to inventing its own)
self.dummyView.translatesAutoresizingMaskIntoConstraints = NO;
self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
self.pageView.translatesAutoresizingMaskIntoConstraints = NO;
self.pageView2.translatesAutoresizingMaskIntoConstraints = NO;
self.pageView3.translatesAutoresizingMaskIntoConstraints = NO;

Related

OSX AVPlayerView resizing when play starts

I have an AVPlayerView as the 2nd NSSplitViewItem in a NSSplitViewController. The 1st split item is a list of videos.
When I select a video, it plays in the AVPlayerView.
The problem is, after I call [self.videoView.player play]; my MediaViewController's view resizes depending on the size of the video being played.
I'm using StoryBoards and my MediaViewController on the canvas has the AVPlayerView Top/Bottom/Trailing/Leading set to hug the superview. This seems to work fine by letting the window be resized and the video adjusts size accordingly.
I don't understand at what point the view decides to resize itself and what to do to stop it.
I have set self.videoView.videoGravity = AVLayerVideoGravityResizeAspect;
What am I missing?
Thanks
EDIT ---------
It seems the problem is not with the video size, but an image overlay I have added to the videoPlayer contentOverlayView.
I add the overlay like this:
if (self.videoView.contentOverlayView.subviews.count == 0)
{
// Add an NSImageView subview to use for overlays
NSImageView *overlayIV = [[NSImageView alloc] initWithFrame:self.videoView.frame];
[overlayIV setTranslatesAutoresizingMaskIntoConstraints:NO];
[overlayIV setImageScaling:NSImageScaleProportionallyUpOrDown];
[self.videoView.contentOverlayView addSubview:overlayIV];
// Set constraints to hug parent
[self.videoView addConstraint:[NSLayoutConstraint constraintWithItem:overlayIV
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.videoView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0]];
[self.videoView addConstraint:[NSLayoutConstraint constraintWithItem:overlayIV
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.videoView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0]];
[self.videoView addConstraint:[NSLayoutConstraint constraintWithItem:overlayIV
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.videoView
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0]];
[self.videoView addConstraint:[NSLayoutConstraint constraintWithItem:overlayIV
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.videoView
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0]];
}
Then when I add a new image overlay it sometimes resizes the window:
NSImageView *overlayIV = [self.videoView.contentOverlayView.subviews firstObject];
if (overlayIV)
{
if (image)
{
overlayIV.image = image;
}
else
{
overlayIV.image = nil;
}
}

visual format constraints in iOs 8

I'm trying to add visual contraints to my view. In iOS 7 everything works fine but with 8.0 no.
My code
NSDictionary *views = NSDictionaryOfVariableBindings(_view1, _view2);
[self.view removeConstraints:self.view.constraints];
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-10-[_view1]-10-|" options:0 metrics:nil views:views];
constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-10-[_view2(200.0)]" options:0 metrics:nil views:views]];
constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-50-[_view1(200.0)]-10-[_view2(200)]" options:0 metrics:nil views:views]];
[self.view addConstraints:constraints];
The view1 after apply contraints has 0 width.
I've searched for apple docs but nothing referes that this method is deprecated.
Any ideas?

How to place 2 NSTextView vertically aligned with a space in between, and make container view resize accordingly with auto layout

Hope the title is clear. Trying to have something below
--------------------
| [titleTextView] |
| | |
| [detailsTextView]|
--------------------
With the code that I tried, the container resized, but both titletextView and detailsTextView are placed together (overlapping each others). I know I init both at (16,0) but shouldn't the constrain place them correctly?
I also get the following error: Unable to simultaneously satisfy constraints:
("<NSLayoutConstraint:0x60000008fcd0 NSTextView:0x600000134be0.bottom == NSView:0x600000134c80.bottom + 20>",
"<NSAutoresizingMaskLayoutConstraint:0x608000093ce0 h=--& v=--& V:[NSTextView:0x600000134be0]-(0)-| (Names: '|':NSView:0x600000134c80 )>")
Code:
//title textView
self.titleTextView = [[NSTextView alloc] initWithFrame:NSMakeRect(16, 0, [self.view frame].size.width - 30, 0)];
[self.titleTextView setEditable:NO];
[self.titleTextView setBackgroundColor:[NSColor clearColor]];
[self.titleTextView setString:#"potentially long text."];
[self.titleTextView setHorizontallyResizable:NO];
[self.titleTextView sizeToFit];
//detail textView
self.detailsTextView = [[NSTextView alloc] initWithFrame:NSMakeRect(16, 0, [self.view frame].size.width - 30, 0)];
[self.detailsTextView setEditable:NO];
[self.detailsTextView setBackgroundColor:[NSColor clearColor]];
[self.detailsTextView setString:#"Very long text."];
[self.detailsTextView setHorizontallyResizable:NO];
[self.detailsTextView sizeToFit];
//Adding to self.view
[self.view addSubview: self.titleTextView];
[self.view addSubview: self.detailsTextView];
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:self.titleTextView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.detailsTextView
attribute:NSLayoutAttributeTop
multiplier:1
constant:20]];
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:self.titleTextView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1
constant:20]];
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:self.detailsTextView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1
constant:20]];
When working with autolayout, you don't want to think about the layout in terms of frames at all. The constraints will determine the frame of your views.
Also, if you're creating the layout in code, you have to call setTranslatesAutoresizingMaskIntoConstraints:NO for the views which you want the autolayout engine to apply to.
So, you'd want to do something like:
UIView* titleTextView = [[UIView alloc] init];
[titleTextView setTranslatesAutoresizingMaskIntoConstraints:NO];
UIView* detailsTextView = [[UIView alloc] init];
[detailsTextView setTranslatesAutoresizingMaskIntoConstraints:NO];
...additional setup stuff...
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|-[titleTextView]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(titleTextView)]];
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|-[detailsTextView]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(detailsTextView)]];
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-[titleTextView][detailsTextView]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(titleTextView, detailsTextView)]];
If working in code, I'd strongly recommend checking out the visual format language which will make setting up constraints much more efficient in code, but you can also do the same thing as above using individual constraints.
Forget all you've done earlier with rects and start thinking in relative positions.
Using my favorite category for autolayout:
https://github.com/jrturton/UIView-Autolayout
you can achieve what you want with these simple constraints (which I find MUCH more readable and intuitive than any of the official API solutions):
[self.titleTextView pinToSuperviewEdges:JRTViewPinLeftEdge|JRTViewPinTopEdge inset:20.0];
[self.detailsTextView pinToSuperviewEdges:JRTViewPinLeftEdge inset:20.0];
[self.detailsTextView pinEdge:NSLayoutAttributeTop toEdge:NSLayoutAttributeBottom ofView:self.titleTextView inset:20];
This will pin both textviews 20 pixels from the left, titleTextView 20 pixels from the top and detailsTextView 20 pixels below titleTextView. Also, the category will add the constraints to the correct view in each case.

How to add fixed width and height constraints between windows content view and NSViewControllers View

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
self.rootVC = [[VZMConversationViewController alloc] initWithNibName:#"VZMConversationViewController" bundle:[NSBundle mainBundle]];
[self.window.contentView addSubview:self.rootVC.view];
self.rootVC.view.frame = ((NSView*)self.window.contentView).bounds;
/*
[NSLayoutConstraint constraintWithItem:self.rootVC.view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.window.contentView attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
[NSLayoutConstraint constraintWithItem:self.rootVC.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.window.contentView attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
//above 2 lines are not working
*/
}
Hey i am an iPhone developer trying to understand mac development, i have the above code and how to add constraints to first view controllers view so that controllers view resizes with the window. Can this be done in XIB?
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
self.rootVC = [[VZMConversationViewController alloc] initWithNibName:#"VZMConversationViewController" bundle:[NSBundle mainBundle]];
NSView *rootView=self.rootVC.view;
[rootView setTranslatesAutoresizingMaskIntoConstraints:NO];
[[[self window] contentView] addSubview:rootView];
[[[self window] contentView] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[rootView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(rootView)]];
[[[self window] contentView] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[rootView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(rootView)]];
}
This fixed the problem

UIScrollView doesn't scroll - even with valid content size and scroll enabled

In my viewDidAppear method (in which i call to the super method) I have the following code:
UIScrollView *navbar = [[UIScrollView alloc] init];
[navbar setScrollEnabled:YES];
[navbar setBackgroundColor:[UIColor redColor]];
[navbar setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:navbar];
NSDictionary *viewsDictionary2 = NSDictionaryOfVariableBindings(navbar);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|[navbar]|"
options:0
metrics:nil
views:viewsDictionary2]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[navbar]|"
options:0
metrics:nil
views:viewsDictionary2]];
NSArray *categories = #[#"nav1", #"nav2", #"nav3", #"nav4", #"nav5", #"nav6", #"nav7", #"nav8", #"nav9", #"nav10", #"nav11", #"nav12", #"nav13"];
NSMutableArray *tempCategoryImages = [NSMutableArray array];
for (NSInteger i = 0; i < categories.count; i++)
{
[tempCategoryImages insertObject:[UIImage imageNamed:categories[i]] atIndex:i];
}
NSArray *categoryImages = [tempCategoryImages copy];
// Add subviews.
//
CGFloat xPadding = 25.0f;
UIView *previousImageView = NULL;
for (NSInteger i = 0; i < categoryImages.count; i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithImage:categoryImages[i]];
[imageView setTranslatesAutoresizingMaskIntoConstraints:NO];
[navbar addSubview:imageView];
if (i == 0) {
//
// First category.
//
[navbar addConstraint:[NSLayoutConstraint constraintWithItem:imageView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:imageView.superview
attribute:NSLayoutAttributeLeft
multiplier:1
constant:0]];
} else {
//
// End categories.
//
[navbar addConstraint:[NSLayoutConstraint constraintWithItem:imageView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:previousImageView
attribute:NSLayoutAttributeRight
multiplier:1
constant:xPadding]];
}
[navbar addConstraint:[NSLayoutConstraint constraintWithItem:imageView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:imageView.superview
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0]];
previousImageView = imageView;
}
// Set content size.
//
CGSize scrollContentSize = CGSizeZero;
for (NSInteger i = 0; i < categories.count; i++)
{
UIImage *tempImage = [UIImage imageNamed:categories[i]];
// Width.
//
scrollContentSize.width += tempImage.size.width;
// Height.
//
if (tempImage.size.height > scrollContentSize.height) {
scrollContentSize.height = tempImage.size.height;
}
}
navbar.contentSize = scrollContentSize;
Which when i log the scrollviews properties, it has the subviews, a large enough content size and scrolling enabled.
Even when I add the UIScrollView to IB and link it to the same code it then works? (I comment out the NSAutoLayout code.
This leads me to believe that for the UIScrollView to work, I cannot use Auto layout.
Am I missing something?
Thanks
EDIT
The code also doesn't work programatically, when I try initWithFrame:frame with setTranslatesAutoresizingMaskIntoConstraints:YES
Check the attributes inspector for your scrollview in IB. Make sure that bounce vertically is checked.
Fixed this problem for me.

Resources