Recently encountered a complex view: In the root controller, there are two sub-controllers, each of which implements a view similar to PageView. Then, each page of PageView is a WebView, and there is a drag-and-drop control in the middle to adjust the size of the views of the two controllers. The reason for using a child controller is to prevent all logic code from getting mixed up in the root controller, so instead of using Nicklockwood’s iCarousel or SwipeView, we use the SCPageViewController we’ve been using. Note a few pits encountered in the automatic layout. About translatesAutoresizingMaskIntoConstraints
Because the view is too complex, so several times forget set translatesAutoresizingMaskIntoConstraints to NO. TranslatesAutoresizingMaskIntoConstraints defaults to YES, that is, according to the default autoresizingMask calculation; Once set to NO, you can use a more flexible tool like Autolayout (or navigation) for automatic layout. When you first start using Autolayout, it can be frustrating to encounter the following warnings. Often overwhelmed and gave up using Autolayout.
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)
(...........)
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.
Copy the code
As stated in the output, the Make a — breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger, Now introduce use UIViewAlertForUnsatisfiableConstraints debug method.
Add — in UIViewAlertForUnsatisfiableConstraints breakpoint: 1. Open the Breakpoint navigation (CMD +7) 2. Click the + button in the lower left corner. 3. Add UIViewAlertForUnsatisfiableConstraints in Symbol
AMBIGUOUS related views are those where constraints are problematic. 0x7F9481C9D990 is the first address of the problematic view. Of course, further debugging requires LLDB commands. For example, print a view object: (LLDB) Po 0x7F9481C9d990 <UIView: 0x7F9481C9d990; frame = (0 0; 768, 359); autoresize = RM+BM; layer = <CALayer: 0x7fc82d338960>>
Change color: (lldb) expr ((UIView *)0x174197010).backgroundColor = [UIColor redColor](UICachedDeviceRGBColor *) $4 = 0x0000000174469cc0
All that’s left is to find the view in the code and change its constraints. Debugging iOS AutoLayout IssuesAutolayout Breakpoints for navigation should be identified. SetNeedsLayout: Tell the page that it needs to be updated, but not immediately. LayoutSubviews is called immediately after execution.
LayoutIfNeeded: Notifies the page layout to be updated immediately. So it’s usually used with setNeedsLayout. You need to call this method if you want to generate a new frame immediately, so you can use this method to animate a general layout animation after updating the layout.
LayoutSubviews: System rewrite layout
SetNeedsUpdateConstraints: told to update, but won’t immediately start
Update constraint updateConstraintsIfNeeded: let us know immediately
UpdateConstraints: system updateConstraints
Basic use of mas_makeConstraints: Add constraints
Mas_updateConstraints: Updates constraints and also adds new constraints
Mas_remakeConstraints: Resets the previous constraints
Note that subviews are added before constraints can be added to child attempts
If you want to use the animation effect, you need the following code:
// Override the updateViewConstraints method, UpdateViewConstraints – (void)updateViewConstraints {[self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) { Make.center.mas_equalto (self.view); Make.width.height.mas_equalto (100 * self.scacle).priorityLow(); // Max zoom to entire view make.width.height.lessThanOrEqualTo(self.view); }]; [super updateViewConstraints]; } / / notification needs to be updated, but not executed immediately [self setNeedsUpdateConstraints]; / / immediately update the constraints to implement dynamic transformation / / update constraints now.so we can animate the change [self updateConstraintsIfNeeded]; [UIView animateWithDuration:0.2 animations:^{[self layoutIfNeeded];}];
After testing, we found a way to directly use the animation layoutIfNeeded after the spiced constraint.
self.button = ({ UIButton *button = [[UIButton alloc] init]; button.backgroundColor = [UIColor orangeColor]; [self.view addSubview:button]; [button mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.view); make.width.height.equalTo(@100); make.top.equalTo(self.blueView.mas_bottom).with.offset(20); }]; @weakify(self); [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) { @strongify(self); [MASConstraintMaker *make] {if (! Self.ispet) { make.top.bottom.left.right.equalTo(self.view).with.insets(UIEdgeInsetsMake(50, 50, 200, 50));} else {make. Center. EqualTo (self. View); make the width, height, equalTo (@ 200);}}]; [UIView animateWithDuration: 0.25 f animations:^{ [self.view layoutIfNeeded]; }]; self.isBigger = !self.isBigger; }]; button; });
The page mentioned above encounters multiple UiscrollViews, which can be a pain when using automatic layout. For details about how to install the navigation automatic layout, see the following steps: complex ScrollView layout, Autolayout layout in UIScrollView, and iOS_AUtolayout_navigation. The main points to note are: Constraints on the UIScrollView itself are added as normal views. The constraints of the inner child controls cannot be set according to UIScrollView and must be complete or they will not support contentSize. Considering those two things, it’s not much different.
You can set it using the auxiliary contentView, [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {make.edges. EqualTo (self.view); // self.view size}]; / / and then set the contentView constraint _contentView backgroundColor = [UIColor greenColor]; [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {make.edges. EqualTo (_scrollView) make.width.equalTo(_scrollView); // width = _scrollView}]; UIView *lastView; CGFloat height = 25; For (int I = 0; int I = 0; int I = 0; i < 10; i++) { UIView *view = [[UIView alloc]init]; view.backgroundColor = [self randomColor]; [_contentView addSubview:view]; [view mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(lastView ? lastView.mas_bottom : // left 0 make.width. EqualTo (_contentView); // left 0 make.width. EqualTo (_contentView); // left 0 make.width. make.height.equalTo(@(height)); // height = height }]; height += 25; lastView = view; }[_contentView mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(lastView); // bottom = lastView}];
However, the calculation was too painful for my project, so I was lazy, because every view from PageView is full of parent view, so I can use the default autoresizingMask for adaptive layout. If it comes to the layout of the iPad, it’s better to use the SizeClass. Constraints add annotations:
SizeClass comments:
Reprinted from: ibloodline.com/articles/20…