/
NSView+DTAutoLayout.m
85 lines (73 loc) · 3.77 KB
/
NSView+DTAutoLayout.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// NSView+DTAutoLayout.m
// DTFoundation
//
// Created by Oliver Drobnik on 26.10.12.
// Copyright (c) 2012 Cocoanetics. All rights reserved.
//
#import "NSView+DTAutoLayout.h"
@implementation NSView (DTAutoLayout)
- (void)addLayoutConstraintWithWidthGreaterOrEqualThan:(CGFloat)width
{
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:width];
constraint.priority = NSLayoutPriorityDragThatCanResizeWindow;
[self addConstraint:constraint];
}
- (void)addLayoutConstraintsForSubview:(NSView *)subview edgeInsets:(NSEdgeInsets)edgeInsets
{
NSParameterAssert(subview);
//NSAssert(subview.superview == self, @"Can only pin a direct subview of the receiver");
// subview cannot have autoresizing mask, that would interfere with these
subview.translatesAutoresizingMaskIntoConstraints = NO;
NSMutableArray *tmpArray = [NSMutableArray array];
if (edgeInsets.left >= 0)
{
// subview's left is x points from superview's left
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:edgeInsets.left];
[tmpArray addObject:constraint];
}
else
{
// subview's left is x points from superview's right
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:edgeInsets.left];
[tmpArray addObject:constraint];
}
if (edgeInsets.right >= 0)
{
// subview's right is x points from superview's right
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:-edgeInsets.right];
[tmpArray addObject:constraint];
}
else
{
// subview's right is x points from superview's left
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:-edgeInsets.right];
[tmpArray addObject:constraint];
}
if (edgeInsets.top >= 0)
{
// subview's top is x points from superview's top
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:edgeInsets.top];
[tmpArray addObject:constraint];
}
else
{
// subview's top is x points from superview's bottom
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:edgeInsets.top];
[tmpArray addObject:constraint];
}
if (edgeInsets.bottom >= 0)
{
// subview's bottom is x points from superview's bottom
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-edgeInsets.bottom];
[tmpArray addObject:constraint];
}
else
{
// subview's bottom is x points from superview's top
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:-edgeInsets.bottom];
[tmpArray addObject:constraint];
}
[self addConstraints:tmpArray];
}
@end