自动布局警告:将尝试通过在 CollectionView 中打破约束 <NSLayoutConstraint 来恢复

Posted

技术标签:

【中文标题】自动布局警告:将尝试通过在 CollectionView 中打破约束 <NSLayoutConstraint 来恢复【英文标题】:Autolayout Warning : Will attempt to recover by breaking constraint <NSLayoutConstraint in CollectionView 【发布时间】:2017-06-06 16:54:40 【问题描述】:

我使用 UICollectionView & Flow Layout 和 Class 来布局 collectionView Cell 是为了实现像 feedly app 或 insorts app 这样的幻灯片动画

import UIKit
class UltravisualLayout: UICollectionViewLayout 
    fileprivate var contentWidth:CGFloat!
    fileprivate var contentHeight:CGFloat!
    fileprivate var yOffset:CGFloat = 0

    var maxAlpha:CGFloat = 1
    var minAlpha:CGFloat = 0

    var widthOffset:CGFloat = 0
    var heightOffset:CGFloat = 0

    fileprivate var cache = [UICollectionViewLayoutAttributes]()

    fileprivate var itemWidth:CGFloat
        return (collectionView?.bounds.width)!
    
    fileprivate var itemHeight:CGFloat
        return (collectionView?.bounds.height)!
    
    fileprivate var collectionViewHeight:CGFloat
        return (collectionView?.bounds.height)!
    


    fileprivate var numberOfItems:Int
        return (collectionView?.numberOfItems(inSection: 0))!
    


    fileprivate var dragOffset:CGFloat
        return (collectionView?.bounds.height)!
    
    fileprivate var currentItemIndex:Int
        return max(0, Int(collectionView!.contentOffset.y / collectionViewHeight))
    

    var nextItemBecomeCurrentPercentage:CGFloat
        return (collectionView!.contentOffset.y / (collectionViewHeight)) - CGFloat(currentItemIndex)
    

    override func prepare() 
        cache.removeAll(keepingCapacity: false)
        yOffset = 0

        for item in 0 ..< numberOfItems

            let indexPath = IndexPath(item: item, section: 0)
            let attribute = UICollectionViewLayoutAttributes(forCellWith: indexPath)
            attribute.zIndex = -(indexPath as NSIndexPath).row


            if ((indexPath as NSIndexPath).item == currentItemIndex+1) && ((indexPath as NSIndexPath).item < numberOfItems)

                attribute.alpha = minAlpha + max((maxAlpha-minAlpha) * nextItemBecomeCurrentPercentage, 0)
                let width = itemWidth - widthOffset + (widthOffset * nextItemBecomeCurrentPercentage)
                let height = itemHeight - heightOffset + (heightOffset * nextItemBecomeCurrentPercentage)

                let deltaWidth =  width/itemWidth
                let deltaHeight = height/itemHeight

                attribute.frame = CGRect(x: 0, y: yOffset, width: itemWidth, height: itemHeight)
                attribute.transform = CGAffineTransform(scaleX: deltaWidth, y: deltaHeight)

                attribute.center.y = (collectionView?.center.y)! +  (collectionView?.contentOffset.y)!
                attribute.center.x = (collectionView?.center.x)! + (collectionView?.contentOffset.x)!
                yOffset += collectionViewHeight

            else
                attribute.frame = CGRect(x: 0, y: yOffset, width: itemWidth, height: itemHeight)
                attribute.center.y = (collectionView?.center.y)! + yOffset
                attribute.center.x = (collectionView?.center.x)!
                yOffset += collectionViewHeight
            
            cache.append(attribute)
        
    

    //Return the size of ContentView
    override var collectionViewContentSize : CGSize 
        contentWidth = (collectionView?.bounds.width)!
        contentHeight = CGFloat(numberOfItems) * (collectionView?.bounds.height)!
        return CGSize(width: contentWidth, height: contentHeight)
    

    //Return Attributes  whose frame lies in the Visible Rect
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? 
        var layoutAttributes = [UICollectionViewLayoutAttributes]()
        for attribute in cache
            if attribute.frame.intersects(rect)
                layoutAttributes.append(attribute)
            
        
        return layoutAttributes
    


    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool 
        return true
    

    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint 
        let itemIndex = round(proposedContentOffset.y / (dragOffset))
        let yOffset = itemIndex * (collectionView?.bounds.height)!
        return CGPoint(x: 0, y: yOffset)
    
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? 

        // Logic that calculates the UICollectionViewLayoutAttributes of the item
        // and returns the UICollectionViewLayoutAttributes
        return UICollectionViewLayoutAttributes(forCellWith: indexPath)
    


我使用 Xib 创建了自定义视图

课堂小故事

#import <UIKit/UIKit.h>
@interface SmallStory : UIView
@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UILabel *pubDate;
@end

SmallStory.m

#import "SmallStory.h"
@interface SmallStory()
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) NSMutableArray *customConstraints;

@end
@implementation SmallStory

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect 
    // Drawing code

*/
- (instancetype)initWithCoder:(NSCoder *)aDecoder

    self = [super initWithCoder:aDecoder];
    if (self) 
        [self customInit];
    
    return self;


- (instancetype)initWithFrame:(CGRect)frame

    self = [super initWithFrame:frame];
    if (self) 
        [self customInit];
    
    return self;


- (void)customInit

    _customConstraints = [[NSMutableArray alloc] init];

    UIView *view = nil;
    NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"SmallStory"
                                                     owner:self
                                                   options:nil];
    for (id object in objects) 
        if ([object isKindOfClass:[UIView class]]) 
            view = object;
            break;
        
    

    if (view != nil) 
        _containerView = view;
        view.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:view];
        [self setNeedsUpdateConstraints];
    

- (void)updateConstraints

    [self removeConstraints:self.customConstraints];
    [self.customConstraints removeAllObjects];

    if (self.containerView != nil) 
        UIView *view = self.containerView;
        NSDictionary *views = NSDictionaryOfVariableBindings(view);
        [self.customConstraints addObjectsFromArray:
         [NSLayoutConstraint constraintsWithVisualFormat:
          @"H:|[view]|" options:0 metrics:nil views:views]];
        [self.customConstraints addObjectsFromArray:
         [NSLayoutConstraint constraintsWithVisualFormat:
          @"V:|[view]|" options:0 metrics:nil views:views]];
        [self addConstraints:self.customConstraints];
    

    [super updateConstraints];




- (UIViewController*)getSuperViewController

    for (UIView* next = [self superview]; next; next = next.superview)
    
        UIResponder* nextResponder = [next nextResponder];

        if ([nextResponder isKindOfClass:[UIViewController class]])
        
            return (UIViewController*)nextResponder;
        
    

    return nil;

@end

Xib 设计的样子

id 分为 5 个部分

每个小视图都被分配为 SmallStory 类

故事板看起来像

当我运行 App 时出现如下自动布局警告

*2017-06-06 22:18:04.044717+0530 Jansatta[1096:258239] [LayoutConstraints] 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:0x17028ca30 h=--& v=--& UIView:0x113d2c230.height == 0   (active)>",
    "<NSLayoutConstraint:0x1702875d0 UIImageView:0x113e32700.bottom == UIView:0x113e32e10.bottomMargin   (active)>",
    "<NSLayoutConstraint:0x170287260 UIImageView:0x113e32700.top == UIView:0x113e32e10.topMargin   (active)>",
    "<NSLayoutConstraint:0x17028af00 SmallStory:0x113e324f0.height == SmallStory:0x113d2c5e0.height   (active)>",
    "<NSLayoutConstraint:0x17028aff0 SmallStory:0x113d2c5e0.height == SmallStory:0x113d2c3d0.height   (active)>",
    "<NSLayoutConstraint:0x17028b270 SmallStory:0x113d2c3d0.height == 0.2*UIView:0x113d2c230.height   (active)>",
    "<NSLayoutConstraint:0x17028bea0 V:|-(0)-[UIView:0x113e32e10]   (active, names: '|':SmallStory:0x113e324f0 )>",
    "<NSLayoutConstraint:0x17028bef0 V:[UIView:0x113e32e10]-(0)-|   (active, names: '|':SmallStory:0x113e324f0 )>"
)
Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1702875d0 UIImageView:0x113e32700.bottom == UIView:0x113e32e10.bottomMargin   (active)>
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.
2017-06-06 22:18:04.047763+0530 Jansatta[1096:258239] [LayoutConstraints] 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:0x17028cad0 h=--& v=--& UIView:0x113d2c230.width == 0   (active)>",
    "<NSLayoutConstraint:0x174285f00 H:|-(10)-[UIImageView:0x113d2c980]   (active, names: '|':UIView:0x113d2d090 )>",
    "<NSLayoutConstraint:0x174285f50 H:[UILabel:0x113d2ce00]-(10)-|   (active, names: '|':UIView:0x113d2d090 )>",
    "<NSLayoutConstraint:0x174285ff0 H:[UIImageView:0x113d2c980]-(10)-[UILabel:0x113d2ce00]   (active)>",
    "<NSLayoutConstraint:0x17028b130 SmallStory:0x113d2c3d0.width == UIView:0x113d2c230.width   (active)>",
    "<NSLayoutConstraint:0x17028b950 H:|-(0)-[UIView:0x113d2d090]   (active, names: '|':SmallStory:0x113d2c3d0 )>",
    "<NSLayoutConstraint:0x17028b9a0 H:[UIView:0x113d2d090]-(0)-|   (active, names: '|':SmallStory:0x113d2c3d0 )>"
)
Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x174285ff0 H:[UIImageView:0x113d2c980]-(10)-[UILabel:0x113d2ce00]   (active)>
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.
2017-06-06 22:18:04.050793+0530 Jansatta[1096:258239] [LayoutConstraints] 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:0x17028ca30 h=--& v=--& UIView:0x113d2c230.height == 0   (active)>",
    "<NSLayoutConstraint:0x170288840 UIImageView:0x113e331c0.bottom == UIView:0x113e33e00.bottomMargin   (active)>",
    "<NSLayoutConstraint:0x1702887a0 UIImageView:0x113e331c0.top == UIView:0x113e33e00.topMargin   (active)>",
    "<NSLayoutConstraint:0x1702892e0 SmallStory:0x113e32fb0.height == SmallStory:0x113e324f0.height   (active)>",
    "<NSLayoutConstraint:0x17028af00 SmallStory:0x113e324f0.height == SmallStory:0x113d2c5e0.height   (active)>",
    "<NSLayoutConstraint:0x17028aff0 SmallStory:0x113d2c5e0.height == SmallStory:0x113d2c3d0.height   (active)>",
    "<NSLayoutConstraint:0x17028b270 SmallStory:0x113d2c3d0.height == 0.2*UIView:0x113d2c230.height   (active)>",
    "<NSLayoutConstraint:0x17028c0d0 V:|-(0)-[UIView:0x113e33e00]   (active, names: '|':SmallStory:0x113e32fb0 )>",
    "<NSLayoutConstraint:0x17028c120 V:[UIView:0x113e33e00]-(0)-|   (active, names: '|':SmallStory:0x113e32fb0 )>"
)
Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x170288840 UIImageView:0x113e331c0.bottom == UIView:0x113e33e00.bottomMargin   (active)>
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.
2017-06-06 22:18:04.054839+0530 Jansatta[1096:258239] [LayoutConstraints] 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:0x17028ca30 h=--& v=--& UIView:0x113d2c230.height == 0   (active)>",
    "<NSLayoutConstraint:0x170287fd0 UIImageView:0x113e341b0.bottom == UIView:0x113e348c0.bottomMargin   (active)>",
    "<NSLayoutConstraint:0x170287f30 UIImageView:0x113e341b0.top == UIView:0x113e348c0.topMargin   (active)>",
    "<NSLayoutConstraint:0x1702892e0 SmallStory:0x113e32fb0.height == SmallStory:0x113e324f0.height   (active)>",
    "<NSLayoutConstraint:0x1702881b0 SmallStory:0x113e32fb0.height == SmallStory:0x113e33fa0.height   (active)>",
    "<NSLayoutConstraint:0x17028af00 SmallStory:0x113e324f0.height == SmallStory:0x113d2c5e0.height   (active)>",
    "<NSLayoutConstraint:0x17028aff0 SmallStory:0x113d2c5e0.height == SmallStory:0x113d2c3d0.height   (active)>",
    "<NSLayoutConstraint:0x17028b270 SmallStory:0x113d2c3d0.height == 0.2*UIView:0x113d2c230.height   (active)>",
    "<NSLayoutConstraint:0x17028c300 V:|-(0)-[UIView:0x113e348c0]   (active, names: '|':SmallStory:0x113e33fa0 )>",
    "<NSLayoutConstraint:0x17028c350 V:[UIView:0x113e348c0]-(0)-|   (active, names: '|':SmallStory:0x113e33fa0 )>"
)
Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x170287fd0 UIImageView:0x113e341b0.bottom == UIView:0x113e348c0.bottomMargin   (active)>
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.
2017-06-06 22:18:04.058339+0530 Jansatta[1096:258239] [LayoutConstraints] 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:0x17028cad0 h=--& v=--& UIView:0x113d2c230.width == 0   (active)>",
    "<NSLayoutConstraint:0x170287620 H:|-(10)-[UIImageView:0x113e32700]   (active, names: '|':UIView:0x113e32e10 )>",
    "<NSLayoutConstraint:0x170287670 H:[UILabel:0x113e32b80]-(10)-|   (active, names: '|':UIView:0x113e32e10 )>",
    "<NSLayoutConstraint:0x1702876c0 H:[UIImageView:0x113e32700]-(10)-[UILabel:0x113e32b80]   (active)>",
    "<NSLayoutConstraint:0x17028aeb0 SmallStory:0x113e324f0.width == SmallStory:0x113d2c5e0.width   (active)>",
    "<NSLayoutConstraint:0x17028b040 SmallStory:0x113d2c5e0.width == SmallStory:0x113d2c3d0.width   (active)>",
    "<NSLayoutConstraint:0x17028b130 SmallStory:0x113d2c3d0.width == UIView:0x113d2c230.width   (active)>",
    "<NSLayoutConstraint:0x17028be00 H:|-(0)-[UIView:0x113e32e10]   (active, names: '|':SmallStory:0x113e324f0 )>",
    "<NSLayoutConstraint:0x17028be50 H:[UIView:0x113e32e10]-(0)-|   (active, names: '|':SmallStory:0x113e324f0 )>"
)
Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1702876c0 H:[UIImageView:0x113e32700]-(10)-[UILabel:0x113e32b80]   (active)>
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.
2017-06-06 22:18:04.061159+0530 Jansatta[1096:258239] [LayoutConstraints] 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:0x17028cad0 h=--& v=--& UIView:0x113d2c230.width == 0   (active)>",
    "<NSLayoutConstraint:0x17028a820 UIImageView:0x113e04f90.width == 180   (active)>",
    "<NSLayoutConstraint:0x17028aaf0 H:|-(10)-[UIImageView:0x113e04f90]   (active, names: '|':UIView:0x113e195a0 )>",
    "<NSLayoutConstraint:0x17028abe0 H:[UILabel:0x113e31dc0]-(10)-|   (active, names: '|':UIView:0x113e195a0 )>",
    "<NSLayoutConstraint:0x17028ac80 H:[UIImageView:0x113e04f90]-(10)-[UILabel:0x113e31dc0]   (active)>",
    "<NSLayoutConstraint:0x17028b040 SmallStory:0x113d2c5e0.width == SmallStory:0x113d2c3d0.width   (active)>",
    "<NSLayoutConstraint:0x17028b130 SmallStory:0x113d2c3d0.width == UIView:0x113d2c230.width   (active)>",
    "<NSLayoutConstraint:0x17028bbd0 H:|-(0)-[UIView:0x113e195a0]   (active, names: '|':SmallStory:0x113d2c5e0 )>",
    "<NSLayoutConstraint:0x17028bc20 H:[UIView:0x113e195a0]-(0)-|   (active, names: '|':SmallStory:0x113d2c5e0 )>"
)
Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17028a820 UIImageView:0x113e04f90.width == 180   (active)>
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.
2017-06-06 22:18:04.064548+0530 Jansatta[1096:258239] [LayoutConstraints] 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:0x17028cad0 h=--& v=--& UIView:0x113d2c230.width == 0   (active)>",
    "<NSLayoutConstraint:0x17028aaf0 H:|-(10)-[UIImageView:0x113e04f90]   (active, names: '|':UIView:0x113e195a0 )>",
    "<NSLayoutConstraint:0x17028abe0 H:[UILabel:0x113e31dc0]-(10)-|   (active, names: '|':UIView:0x113e195a0 )>",
    "<NSLayoutConstraint:0x17028ac80 H:[UIImageView:0x113e04f90]-(10)-[UILabel:0x113e31dc0]   (active)>",
    "<NSLayoutConstraint:0x17028b040 SmallStory:0x113d2c5e0.width == SmallStory:0x113d2c3d0.width   (active)>",
    "<NSLayoutConstraint:0x17028b130 SmallStory:0x113d2c3d0.width == UIView:0x113d2c230.width   (active)>",
    "<NSLayoutConstraint:0x17028bbd0 H:|-(0)-[UIView:0x113e195a0]   (active, names: '|':SmallStory:0x113d2c5e0 )>",
    "<NSLayoutConstraint:0x17028bc20 H:[UIView:0x113e195a0]-(0)-|   (active, names: '|':SmallStory:0x113d2c5e0 )>"
)
Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17028ac80 H:[UIImageView:0x113e04f90]-(10)-[UILabel:0x113e31dc0]   (active)>
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.
2017-06-06 22:18:04.066582+0530 Jansatta[1096:258239] [LayoutConstraints] 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:0x17028ca30 h=--& v=--& UIView:0x113d2c230.height == 0   (active)>",
    "<NSLayoutConstraint:0x17028aaa0 UIImageView:0x113e04f90.bottom == UIView:0x113e195a0.bottomMargin   (active)>",
    "<NSLayoutConstraint:0x17028ab40 UIImageView:0x113e04f90.top == UIView:0x113e195a0.topMargin   (active)>",
    "<NSLayoutConstraint:0x17028aff0 SmallStory:0x113d2c5e0.height == SmallStory:0x113d2c3d0.height   (active)>",
    "<NSLayoutConstraint:0x17028b270 SmallStory:0x113d2c3d0.height == 0.2*UIView:0x113d2c230.height   (active)>",
    "<NSLayoutConstraint:0x17028bc70 V:|-(0)-[UIView:0x113e195a0]   (active, names: '|':SmallStory:0x113d2c5e0 )>",
    "<NSLayoutConstraint:0x17028bcc0 V:[UIView:0x113e195a0]-(0)-|   (active, names: '|':SmallStory:0x113d2c5e0 )>"
)
Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17028aaa0 UIImageView:0x113e04f90.bottom == UIView:0x113e195a0.bottomMargin   (active)>
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.
2017-06-06 22:18:04.068461+0530 Jansatta[1096:258239] [LayoutConstraints] 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:0x17028ca30 h=--& v=--& UIView:0x113d2c230.height == 0   (active)>",
    "<NSLayoutConstraint:0x174285e60 UIImageView:0x113d2c980.bottom == UIView:0x113d2d090.bottomMargin   (active)>",
    "<NSLayoutConstraint:0x174285eb0 UIImageView:0x113d2c980.top == UIView:0x113d2d090.topMargin   (active)>",
    "<NSLayoutConstraint:0x17028b270 SmallStory:0x113d2c3d0.height == 0.2*UIView:0x113d2c230.height   (active)>",
    "<NSLayoutConstraint:0x17028b9f0 V:|-(0)-[UIView:0x113d2d090]   (active, names: '|':SmallStory:0x113d2c3d0 )>",
    "<NSLayoutConstraint:0x17028ba40 V:[UIView:0x113d2d090]-(0)-|   (active, names: '|':SmallStory:0x113d2c3d0 )>"
)
Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x174285e60 UIImageView:0x113d2c980.bottom == UIView:0x113d2d090.bottomMargin   (active)>
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.
2017-06-06 22:18:04.070615+0530 Jansatta[1096:258239] [LayoutConstraints] 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:0x17028cad0 h=--& v=--& UIView:0x113d2c230.width == 0   (active)>",
    "<NSLayoutConstraint:0x1702873f0 UIImageView:0x113e331c0.width == 180   (active)>",
    "<NSLayoutConstraint:0x170287760 H:|-(10)-[UIImageView:0x113e331c0]   (active, names: '|':UIView:0x113e33e00 )>",
    "<NSLayoutConstraint:0x1702886b0 H:[UILabel:0x113e338d0]-(10)-|   (active, names: '|':UIView:0x113e33e00 )>",
    "<NSLayoutConstraint:0x170288570 H:[UIImageView:0x113e331c0]-(10)-[UILabel:0x113e338d0]   (active)>",
    "<NSLayoutConstraint:0x1702884d0 UILabel:0x113e338d0.leading == UILabel:0x113e333b0.leading   (active)>",
    "<NSLayoutConstraint:0x170288390 UILabel:0x113e333b0.trailing == UILabel:0x113e338d0.trailing   (active)>",
    "<NSLayoutConstraint:0x17028ae60 SmallStory:0x113e32fb0.width == SmallStory:0x113e324f0.width   (active)>",
    "<NSLayoutConstraint:0x17028aeb0 SmallStory:0x113e324f0.width == SmallStory:0x113d2c5e0.width   (active)>",
    "<NSLayoutConstraint:0x17028b040 SmallStory:0x113d2c5e0.width == SmallStory:0x113d2c3d0.width   (active)>",
    "<NSLayoutConstraint:0x17028b130 SmallStory:0x113d2c3d0.width == UIView:0x113d2c230.width   (active)>",
    "<NSLayoutConstraint:0x17028c030 H:|-(0)-[UIView:0x113e33e00]   (active, names: '|':SmallStory:0x113e32fb0 )>",
    "<NSLayoutConstraint:0x17028c080 H:[UIView:0x113e33e00]-(0)-|   (active, names: '|':SmallStory:0x113e32fb0 )>"
)*

如何解决请帮忙

【问题讨论】:

你为什么使用 5 个视图? 它需要我们的应用程序,如果任何其他方式那么请建议 您是否尝试过警告提示的内容? 嘿@vadian 我尝试了很多次为自动布局定义约束但没有成功。 【参考方案1】:

嗨,我解决了我的问题,

从必需到高优先级的 Xib 约束警告消失。

【讨论】:

这会消除警告,但不能解决实际问题。【参考方案2】:

使用 stackView 而不是 5 个 UIView。

为什么?

StackView 比您现在创建的 UIView 效果更好。

您可以轻松地放入任何视图、隐藏、显示。无需考虑 stackView 中视图之间的自动布局。

我刚刚使用一个 stackView 制作了您所做的。只需查看层次结构

我刚刚在检查器中设置了同样的填充

并且输出没有自动布局问题。只需将左上角右下角约束设置为 0 即可 stackView 的 superView

【讨论】:

对于这五个视图,我有一个自定义的 xib 和 SmallStoryClass 有问题。我也试过这个,但出现警告 嘿@elk_cloner 你是对的,但我有最小的 sdk ios 8 所以目前我不能使用这个功能。

以上是关于自动布局警告:将尝试通过在 CollectionView 中打破约束 <NSLayoutConstraint 来恢复的主要内容,如果未能解决你的问题,请参考以下文章

自动布局中的错位视图警告

自动首选最大布局宽度在 8.0 之前的 iOS 版本中不可用

自动布局约束警告

自动布局粘在底部

未显示情节提要警告

在导航栏中使用提示时自动布局警告