【问题标题】:Failed to update the auto layout status: The agent crashed自动布局状态更新失败:代理崩溃
【发布时间】:2018-03-07 23:16:22
【问题描述】:

我正在使用自定义类进行分段控制,但出现两个错误:

“更新自动布局状态失败:代理崩溃”

“未能呈现 ADVSegmentedControl 的实例:代理崩溃”。

我正在使用 Xcode 7.1 和 Swift 2.0。代码如下:

//  ADVSegmentedControl.swift
//  Mega
//
//  Created by Tope Abayomi on 01/12/2014.
//  Copyright (c) 2014 App Design Vault. All rights reserved.
//
import UIKit
@IBDesignable public class ADVSegmentedControl: UIControl {
    public var labels = [UILabel]()
    var thumbView = UIView()
    var items: [String] = ["item1", "item2", "item3", "item4", "item5"] {
        didSet {
            setupLabels()
        }
    }
    var bandera = true
    var selectedIndex : Int = 0 {
        didSet {
            displayNewSelectedIndex()
        }
    }
    //
    required public init(WithFramea frame: CGRect){
        super.init(frame: frame)
        setupView()
    }
    //
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        setupView()
    }

    @IBInspectable public var selectedLabelColor : UIColor = UIColor.whiteColor() {
        didSet {
            setSelectedColors()
        }
    }
    @IBInspectable public var unselectedLabelColor : UIColor = UIColor.whiteColor() {
        didSet {
            setSelectedColors()
        }
    }
    @IBInspectable public var thumbColor : UIColor = UIColor(patternImage: UIImage(named: "cafeFuerte.png")!) {
        didSet {
            setSelectedColors()
        }
    }
    @IBInspectable public var borderColor : UIColor = UIColor.whiteColor() {
        didSet {
            layer.borderColor = borderColor.CGColor
        }
    }
    @IBInspectable public var font : UIFont! = UIFont.systemFontOfSize(7) {
        didSet {
            setFont()
        }
    }
    func setupView(){
        layer.cornerRadius = frame.height / 2
        layer.borderColor = UIColor.whiteColor().CGColor
        //UIColor(white: 1.0, alpha: 0.5).CGColor
        layer.borderWidth = 1
        backgroundColor = UIColor(patternImage: UIImage(named: "cafeClaro.png")!)
        setupLabels()
        addIndividualItemConstraints(labels, mainView: self, padding: 0)
        insertSubview(thumbView, atIndex: 0)
    }
    func setupLabels(){
        for label in labels {
            label.removeFromSuperview()
        }
        labels.removeAll(keepCapacity: true)
        for index in 1...items.count {

            let label = UILabel(frame: CGRectMake(0, 0, 70, 40))
            label.text = items[index - 1]
            label.backgroundColor = UIColor.clearColor()
//          UIColor(red: 167.0, green: 121.0, blue: 66.0, alpha: 1.0)
            label.textAlignment = .Center
//            label.font = UIFont(name: "Avenir-Black", size: 15)
            label.font = UIFont(name: "System", size: 15)
            label.textColor = index == 1 ? selectedLabelColor : unselectedLabelColor
            label.translatesAutoresizingMaskIntoConstraints = false
            self.addSubview(label)
            labels.append(label)
        }
        addIndividualItemConstraints(labels, mainView: self, padding: 0)
    }
    override public func layoutSubviews() {
        super.layoutSubviews()

        var selectFrame = self.bounds
        let newWidth = CGRectGetWidth(selectFrame) / CGFloat(items.count)
        selectFrame.size.width = newWidth
        thumbView.frame = selectFrame
        thumbView.backgroundColor = thumbColor
        thumbView.layer.cornerRadius = thumbView.frame.height / 2
        displayNewSelectedIndex()
    }
    override public func beginTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool {
        let location = touch.locationInView(self)
        var calculatedIndex : Int?
        for (index, item) in labels.enumerate() {
            if item.frame.contains(location) {
                calculatedIndex = index
            }
        }
        if calculatedIndex != nil {
            selectedIndex = calculatedIndex!
            sendActionsForControlEvents(.ValueChanged)
        }
        return false
    }
    func displayNewSelectedIndex(){
        for (_, item) in labels.enumerate() {
            item.textColor = unselectedLabelColor
        }

        let label = labels[selectedIndex]
        label.textColor = selectedLabelColor

        UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.8, options: UIViewAnimationOptions.CurveEaseInOut, animations: {


            self.thumbView.frame = label.frame

            }, completion: nil)
    }
    func addIndividualItemConstraints(items: [UIView], mainView: UIView, padding: CGFloat) {
        for (index, button) in items.enumerate() {
            let topConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: mainView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0)

            let bottomConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: mainView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0)
            var rightConstraint : NSLayoutConstraint!
            if index == items.count - 1 {
                rightConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: mainView, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: -padding)
            }else{
                let nextButton = items[index+1]
                rightConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: nextButton, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: -padding)
            }
            var leftConstraint : NSLayoutConstraint!
            if index == 0 {

                leftConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: mainView, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: padding)
            }else{
                let prevButton = items[index-1]
                leftConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: prevButton, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: padding)
                let firstItem = items[0]
                let widthConstraint = NSLayoutConstraint(item: button, attribute: .Width, relatedBy: NSLayoutRelation.Equal, toItem: firstItem, attribute: .Width, multiplier: 1.0  , constant: 0)
                mainView.addConstraint(widthConstraint)
            }
            mainView.addConstraints([topConstraint, bottomConstraint, rightConstraint, leftConstraint])
        }
    }
    func setSelectedColors(){
        for item in labels {
            item.textColor = unselectedLabelColor
        }
        if labels.count > 0 {
            labels[0].textColor = selectedLabelColor
        }
        thumbView.backgroundColor = thumbColor
    }
    func setFont(){
        for item in labels {
            item.font = font
        }
    }
}

【问题讨论】:

  • 我觉得有问题:"init?(coder aDecoder: NSCoder)"

标签: ios swift


【解决方案1】:

错误在:

UIColor(patternImage: UIImage(named: "cafeClaro.png")!)

更改为:

UIColor(红色:182/255,绿色:139/255,蓝色:84/255,alpha:1.00)

或其他任何东西,并且运行良好;)

【讨论】:

  • 这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方发表评论 - 您可以随时对自己的帖子发表评论,一旦您earn足够reputation,您就可以comment on any post。如果您有一个相关但不同的问题,ask a new question 参考这个问题,如果它有助于提供上下文。
猜你喜欢
  • 2020-06-29
  • 2019-11-17
  • 2017-02-05
  • 1970-01-01
  • 2018-05-11
  • 2012-10-17
  • 2015-02-07
  • 2019-11-29
相关资源
最近更新 更多