99
1010@implementation ACRContentHoldingUIScrollView
1111
12+ - (void )preconfigreAutolayout
13+ {
14+ if (!self.contentview ) {
15+ return ;
16+ }
17+
18+ self.translatesAutoresizingMaskIntoConstraints = NO ;
19+ self.stretchConstraints = [[NSMutableArray alloc ] init ];
20+ self.nonStretchConstraints = [[NSMutableArray alloc ] init ];
21+
22+ [(NSMutableArray *)self .stretchConstraints addObject: [self .heightAnchor constraintEqualToAnchor: self .contentview.heightAnchor]];
23+ [(NSMutableArray *)self .nonStretchConstraints addObject: [self .heightAnchor constraintEqualToAnchor: self .contentview.heightAnchor]];
24+
25+ [(NSMutableArray *)self .stretchConstraints addObjectsFromArray: @[
26+ [self .contentview.widthAnchor constraintEqualToAnchor: self .widthAnchor
27+ multiplier: 1.0 ],
28+ [self .contentview.centerXAnchor constraintEqualToAnchor: self .centerXAnchor],
29+ [self .contentview.centerYAnchor constraintEqualToAnchor: self .centerYAnchor]
30+ ]];
31+
32+ [(NSMutableArray *)self .nonStretchConstraints addObjectsFromArray: @[
33+ [NSLayoutConstraint constraintWithItem: self
34+ attribute: NSLayoutAttributeTop
35+ relatedBy: NSLayoutRelationEqual
36+ toItem: self .contentview
37+ attribute: NSLayoutAttributeTop
38+ multiplier: 1.0
39+ constant: 0 ],
40+ [NSLayoutConstraint constraintWithItem: self
41+ attribute: NSLayoutAttributeBottom
42+ relatedBy: NSLayoutRelationEqual
43+ toItem: self .contentview
44+ attribute: NSLayoutAttributeBottom
45+ multiplier: 1.0
46+ constant: 0 ],
47+ [NSLayoutConstraint constraintWithItem: self
48+ attribute: NSLayoutAttributeLeading
49+ relatedBy: NSLayoutRelationEqual
50+ toItem: self .contentview
51+ attribute: NSLayoutAttributeLeading
52+ multiplier: 1.0
53+ constant: 0 ],
54+ [NSLayoutConstraint constraintWithItem: self
55+ attribute: NSLayoutAttributeTrailing
56+ relatedBy: NSLayoutRelationEqual
57+ toItem: self .contentview
58+ attribute: NSLayoutAttributeTrailing
59+ multiplier: 1.0
60+ constant: 0 ]
61+ ]];
62+ }
63+
1264- (CGSize)intrinsicContentSize
1365{
1466 // whenever intrinsic content size is called, re-check if content size for subviews have changed
@@ -36,17 +88,12 @@ - (void)layoutSubviews
3688 self.contentWidth = [self intrinsicContentSize ].width ;
3789
3890 // if content size is smaller than the superview, and stretch is specified, stretch content view
39- if (self.contentview && (self.stretch && self.frame .size .width > self.contentWidth && !self.widthConstraintForStretch .active )) {
91+ if (self.contentview && (self.stretch && self.frame .size .width > self.contentWidth &&
92+ self.stretchConstraints &&
93+ self.stretchConstraints .count > 0 &&
94+ !self.stretchConstraints [0 ].active )) {
4095 // add new constraints before layoutSubview before layout pass
41- self.widthConstraintForStretch = [self .contentview.widthAnchor constraintEqualToAnchor: self .widthAnchor multiplier: 1.0 ];
42- self.widthConstraintForStretch .active = YES ;
43-
44- self.centerXConstraintForStretch = [self .contentview.centerXAnchor constraintEqualToAnchor: self .centerXAnchor];
45- self.centerXConstraintForStretch .active = YES ;
46-
47- self.centerYConstraintForStretch = [self .contentview.centerYAnchor constraintEqualToAnchor: self .centerYAnchor];
48- self.centerYConstraintForStretch .active = YES ;
49-
96+ [NSLayoutConstraint activateConstraints: self .stretchConstraints];
5097 // layout pass
5198 [super layoutSubviews ];
5299 // everything is done
@@ -58,53 +105,8 @@ - (void)layoutSubviews
58105 if ((self.contentview .axis == UILayoutConstraintAxisHorizontal) && self.frame .size .width < self.contentWidth && !_isContentSizeConstraintSet) {
59106 _isContentSizeConstraintSet = YES ;
60107 // de-activate constraints
61- if (self.widthConstraintForStretch ) {
62- self.widthConstraintForStretch .active = NO ;
63- }
64-
65- if (self.centerXConstraintForStretch ) {
66- self.centerXConstraintForStretch .active = NO ;
67- }
68-
69- if (self.centerYConstraintForStretch ) {
70- self.centerYConstraintForStretch .active = NO ;
71- }
72-
73- // following constraints enables scrolling behaviors
74- [NSLayoutConstraint constraintWithItem: self
75- attribute: NSLayoutAttributeTop
76- relatedBy: NSLayoutRelationEqual
77- toItem: self .contentview
78- attribute: NSLayoutAttributeTop
79- multiplier: 1.0
80- constant: 0 ]
81- .active = YES ;
82- [NSLayoutConstraint constraintWithItem: self
83- attribute: NSLayoutAttributeBottom
84- relatedBy: NSLayoutRelationEqual
85- toItem: self .contentview
86- attribute: NSLayoutAttributeBottom
87- multiplier: 1.0
88- constant: 0 ]
89- .active = YES ;
90- [NSLayoutConstraint constraintWithItem: self
91- attribute: NSLayoutAttributeLeading
92- relatedBy: NSLayoutRelationEqual
93- toItem: self .contentview
94- attribute: NSLayoutAttributeLeading
95- multiplier: 1.0
96- constant: 0 ]
97- .active = YES ;
98- [NSLayoutConstraint constraintWithItem: self
99- attribute: NSLayoutAttributeTrailing
100- relatedBy: NSLayoutRelationEqual
101- toItem: self .contentview
102- attribute: NSLayoutAttributeTrailing
103- multiplier: 1.0
104- constant: 0 ]
105- .active = YES ;
106-
107-
108+ [NSLayoutConstraint deactivateConstraints: self .stretchConstraints];
109+ [NSLayoutConstraint activateConstraints: self .nonStretchConstraints];
108110 // now ready for layout pass
109111 [super layoutSubviews ];
110112 }
0 commit comments