【问题标题】:Collection view not obeying constraints in Xcode集合视图不遵守 Xcode 中的约束
【发布时间】:2018-09-13 16:07:24
【问题描述】:

我有一个集合视图,它的边缘被限制在安全区域。但是,集合视图完全覆盖了整个屏幕。现在,如果我更改约束删除顶部约束并将其更改为固定高度约束,集合视图仍然会忽略它并填充整个屏幕。当我删除所有约束时,集合视图不会填满整个屏幕。此错误仅影响顶部约束,所有其他约束都可以正常工作。

更新: 当我在 headerView 补充视图委托函数中注释掉一些行时,它再次起作用:

  func pressedColour(colour: UIColor){

        let origImage = self.imageView?.image
        let tintedImage = origImage?.withRenderingMode(.alwaysTemplate)
        self.setImage(tintedImage, for: .highlighted)
        //self.imageView?.contentMode = .scaleAspectFill
        self.tintColor = colour
    }

我注释掉的行调用了该函数。自动布局错误是

2018-09-13 23:38:16.556318+0100 DoppelChat[59765:15398941] [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. 
(
    "<NSLayoutConstraint:0x60c00028a780 UICollectionView:0x7fac548e6600.top == UILayoutGuide:0x60c0001bea00'UIViewSafeAreaLayoutGuide'.top   (active)>",
    "<NSAutoresizingMaskLayoutConstraint:0x60c0004892e0 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' UIView:0x7fac59a7e150.minY == 0   (active, names: '|':_UIParallaxDimmingView:0x7fac59a7eb90 )>",
    "<NSLayoutConstraint:0x60c00048b4a0 'UIViewSafeAreaLayoutGuide-top' V:|-(64)-[UILayoutGuide:0x60c0001bea00'UIViewSafeAreaLayoutGuide']   (active, names: '|':UIView:0x7fac59a7e150 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60c00028a780 UICollectionView:0x7fac548e6600.top == UILayoutGuide:0x60c0001bea00'UIViewSafeAreaLayoutGuide'.top   (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.

我已将范围缩小到导致问题的这一行:

let origImage = self.imageView?.image

【问题讨论】:

  • 不,只是一个普通的视图控制器
  • 也没有什么可以显示的约束,它只是常规对齐到顶部
  • 好的,我添加了约束

标签: xcode uicollectionview ios-autolayout


【解决方案1】:

好的,我解决了,如果它对你有意义,我用这个函数来代替按钮按下的颜色:

func pressedColour(colour: UIColor, uiImage: UIImage?,  alphaMultiplier: CGFloat){
        let origImage = uiImage
        let tintedImage = origImage?.withRenderingMode(.alwaysTemplate)
        self.setImage(tintedImage, for: .highlighted)
        var red: CGFloat = 0
        var green: CGFloat = 0
        var blue: CGFloat = 0
        var alpha: CGFloat = 0
        colour.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
        self.tintColor = UIColor(red: red, green: green, blue: blue, alpha: alpha * alphaMultiplier)
    }

我传入的 uiImage 参数是图像文字而不是 button.imageview.image

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多