无法更新自动布局状态:代理崩溃

时间:2016-01-18 02:58:16

标签: ios swift

我使用自定义类进行分段控制,我收到两个错误:

  

“无法更新自动布局状态:代理程序崩溃”

  

“无法呈现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
        }
    }
}

1 个答案:

答案 0 :(得分:0)

错误在:

  

UIColor(patternImage:UIImage(名称:" cafeClaro.png")!)

更改为:

  

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

或者其他什么,并且出色地运行;)