我使用UICollectionView &流式佈局和班的CollectionView細胞佈局恢復是實現幻燈片動畫像資訊提供ly APP或insorts應用自動佈局警告:將嘗試打破約束<NSLayoutConstraint中的CollectionView
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作爲
類SmallStory
#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的設計看起來像
並且每個小視圖被分配爲SmallStory類
和故事板外貌像
當運行應用程序有自動佈局警告如下
2017年6月6日22:18:04.044717 + 0530 Jansatta [1096:258239] [LayoutConstraints]無法同時滿足約束。 以下列表中的至少一個約束可能是您不想要的。 試試這個: (1)看看每個約束,並試圖找出你不期望的; (2)找到添加不需要的約束或約束並修復它的代碼。 (注意:如果你看到,你不明白NSAutoresizingMaskLayoutConstraints,請參閱文檔UIView的財產translatesAutoresizingMaskIntoConstraints) ( 「」, 「」, 「」, 「」, 「」, 「」 「」, 「」 ) 將嘗試打破約束 請在UIViewAlertForUnsatisfiableConstraints一個象徵性的斷點,以趕上這在調試器來恢復。 列出的UIView上的UIConstraintBasedLayoutDebugging類中的方法也可能有所幫助。 2017-06-06 22:18:04.047763 + 0530 Jansatta [1096:258239] [LayoutConstraints]無法同時滿足約束條件。 以下列表中的至少一個約束可能是您不想要的。 試試這個: (1)看看每個約束,並試圖找出你不期望的; (2)找到添加不需要的約束或約束並修復它的代碼。 (注意:如果你看到,你不明白NSAutoresizingMaskLayoutConstraints,請參閱文檔UIView的財產translatesAutoresizingMaskIntoConstraints) ( 「」, 「」, 「」, 「」, 「」, 「」, 「」 ) 將嘗試通過中斷約束來恢復 在UIViewAlertForUnsatisfiableConstraints中創建一個符號斷點,以便在調試器中捕獲此斷點。 列出的UIView上的UIConstraintBasedLayoutDebugging類中的方法也可能有所幫助。 2017-06-06 22:18:04.050793 + 0530 Jansatta [1096:258239] [LayoutConstraints]無法同時滿足約束條件。 以下列表中的至少一個約束可能是您不想要的。 試試這個: (1)看看每個約束,並試圖找出你不期望的; (2)找到添加不需要的約束或約束並修復它的代碼。 (注意:如果你看到,你不明白NSAutoresizingMaskLayoutConstraints,請參閱文檔UIView的財產translatesAutoresizingMaskIntoConstraints) ( 「」, 「」, 「」, 「」, 「」, 「」 「」, 「」, 「」 ) 將嘗試打破約束 請在UIViewAlertForUnsatisfiableConstraints一個象徵性的斷點,以趕上這在調試器來恢復。 列出的UIView上的UIConstraintBasedLayoutDebugging類中的方法也可能有所幫助。 2017-06-06 22:18:04.054839 + 0530 Jansatta [1096:258239] [LayoutConstraints]無法同時滿足約束條件。 以下列表中的至少一個約束可能是您不想要的。 試試這個: (1)看看每個約束,並試圖找出你不期望的; (2)找到添加不需要的約束或約束並修復它的代碼。 (注意:如果你看到,你不明白NSAutoresizingMaskLayoutConstraints,請參閱文檔UIView的財產translatesAutoresizingMaskIntoConstraints) ( 「」, 「」, 「」, 「」, 「」, 「」 「」, 「」, 「」, 「」 ) 將嘗試打破約束 請在UIViewAlertForUnsatisfiableConstraints一個象徵性的斷點,以趕上這在調試器來恢復。 列出的UIView上的UIConstraintBasedLayoutDebugging類中的方法也可能有所幫助。 2017-06-06 22:18:04.058339 + 0530 Jansatta [1096:258239] [LayoutConstraints]無法同時滿足約束條件。 以下列表中的至少一個約束可能是您不想要的。 試試這個: (1)看看每個約束,並試圖找出你不期望的; (2)找到添加不需要的約束或約束並修復它的代碼。 (注意:如果你看到,你不明白NSAutoresizingMaskLayoutConstraints,請參閱文檔UIView的財產translatesAutoresizingMaskIntoConstraints) ( 「」, 「」, 「」, 「」, 「」, 「」 「」, 「」, 「」 ) 將嘗試打破約束 請在UIViewAlertForUnsatisfiableConstraints一個象徵性的斷點,以趕上這在調試器來恢復。 列出的UIView上的UIConstraintBasedLayoutDebugging類中的方法也可能有所幫助。 2017-06-06 22:18:04.061159 + 0530 Jansatta [1096:258239] [LayoutConstraints]無法同時滿足約束條件。 以下列表中的至少一個約束可能是您不想要的。 試試這個: (1)看看每個約束,並試圖找出你不期望的; (2)找到添加不需要的約束或約束並修復它的代碼。 (注意:如果你看到,你不明白NSAutoresizingMaskLayoutConstraints,請參閱文檔UIView的財產translatesAutoresizingMaskIntoConstraints) ( 「」, 「」, 「」, 「」, 「」, 「」 「」, 「」, 「」 ) 將嘗試打破約束 請在UIViewAlertForUnsatisfiableConstraints一個象徵性的斷點,以趕上這在調試器來恢復。 列出的UIView上的UIConstraintBasedLayoutDebugging類中的方法也可能有所幫助。 2017-06-06 22:18:04.064548 + 0530 Jansatta [1096:258239] [LayoutConstraints]無法同時滿足約束條件。 以下列表中的至少一個約束可能是您不想要的。 試試這個: (1)看看每個約束,並試圖找出你不期望的; (2)找到添加不需要的約束或約束並修復它的代碼。 (注意:如果你看到,你不明白NSAutoresizingMaskLayoutConstraints,請參閱文檔UIView的財產translatesAutoresizingMaskIntoConstraints) ( 「」, 「」, 「」, 「」, 「」, 「」 「」, 「」 ) 將嘗試打破約束 請在UIViewAlertForUnsatisfiableConstraints一個象徵性的斷點,以趕上這在調試器來恢復。 列出的UIView上的UIConstraintBasedLayoutDebugging類中的方法也可能有所幫助。 2017-06-06 22:18:04.066582 + 0530 Jansatta [1096:258239] [LayoutConstraints]無法同時滿足約束條件。 以下列表中的至少一個約束可能是您不想要的。 試試這個: (1)看看每個約束,並試圖找出你不期望的; (2)找到添加不需要的約束或約束並修復它的代碼。 (注意:如果你看到,你不明白NSAutoresizingMaskLayoutConstraints,請參閱文檔UIView的財產translatesAutoresizingMaskIntoConstraints) ( 「」, 「」, 「」, 「」, 「」, 「」, 「」 ) 將嘗試通過中斷約束來恢復 在UIViewAlertForUnsatisfiableConstraints中創建一個符號斷點,以便在調試器中捕獲此斷點。 列出的UIView上的UIConstraintBasedLayoutDebugging類中的方法也可能有所幫助。 2017-06-06 22:18:04.068461 + 0530 Jansatta [1096:258239] [LayoutConstraints]無法同時滿足約束條件。 以下列表中的至少一個約束可能是您不想要的。 試試這個: (1)看看每個約束,並試圖找出你不期望的; (2)找到添加不需要的約束或約束並修復它的代碼。 (注意:如果你看到,你不明白NSAutoresizingMaskLayoutConstraints,請參閱文檔UIView的財產translatesAutoresizingMaskIntoConstraints) ( 「」, 「」, 「」, 「」, 「」, 「」 ) 將嘗試通過中斷約束來恢復 在UIViewAlertForUnsatisfiableConstraints中創建一個符號斷點,以便在調試器中捕獲此斷點。 列出的UIView上的UIConstraintBasedLayoutDebugging類中的方法也可能有所幫助。 2017-06-06 22:18:04.070615 + 0530 Jansatta [1096:258239] [LayoutConstraints]無法同時滿足約束條件。 以下列表中的至少一個約束可能是您不想要的。 試試這個: (1)看看每個約束,並試圖找出你不期望的; (2)找到添加不需要的約束或約束並修復它的代碼。 (注意:如果你看到,你不明白NSAutoresizingMaskLayoutConstraints,請參閱文檔UIView的財產translatesAutoresizingMaskIntoConstraints) ( 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」 )
如何解決這個PLEASE HELP
你爲什麼用5個視圖? –
它需要我們的應用程序,如果任何其他方式然後PLZ建議 – user2856484
你試過什麼警告建議? – vadian