无法为自定义视图设置约束,Swift

时间:2017-09-25 07:18:42

标签: swift uitableview nslayoutconstraint

我无法为tableViewCell中的自定义视图设置前导和顶部约束,我还收到一条错误消息,上面写着

  

profileImageView

的大小不明确

但是我可以将profileImageView置于单元格的中心,但是当我尝试设置leadingAnchor时(如下面的函数setUp所示),正在使用视图的中心而不是leadingAnchor。

这是我的profileImageView的显示方式,它会移到单元格的左上角。

enter image description here

class CustomCell: UITableViewCell {

var profileImageView        : ProfileImageView!    

func setUp(_viewController : UIViewController){

    let profileImageFrame   = CGRect(x: 0, y: 0, width: 150, height: 150) // Creating a frame for the imageView
            profileImageView        =   ProfileImageView(frame: profileImageFrame, viewController: _viewController, photoWidth: 100.0)

    profileImageView.translatesAutoresizingMaskIntoConstraints = false

    contentView.addSubview(profileImageView) // ContentView refers to the cell contentView

    profileImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true

    profileImageView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true

}

}

使用以下类创建profileImageView

class ProfileImageView: UIView,
                        UIImagePickerControllerDelegate,
                        UINavigationControllerDelegate,
                        TOCropViewControllerDelegate{
    // FOR IMAGE CROPPER
    var photoDiameter:  CGFloat           =   100.0
    var photoFrameViewPadding : CGFloat   =   2.0
    let addButtonTitle = "add\nphoto"

    var didSetupConstraints: Bool = false

    var photoFrameView          :   UIView!
    var addPhotoButton          :   UIButton!

    var editPhotoButton         :   UIButton!
    var currentViewController   =   UIViewController()


    init(frame: CGRect, viewController : UIViewController, photoWidth: CGFloat){
        //Initialise values
        photoFrameView              =   UIView()
        addPhotoButton              =   UIButton()
        editPhotoButton             =   UIButton()
        currentViewController       =   viewController
        photoDiameter               =   photoWidth
        super.init(frame: frame)
        setup()
    }


    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }



    func setup(){


        // ---------------------------
        // Add the frame of the photo.
        // ---------------------------
        let photoImageViewOrigin = self.frame.origin
        self.frame = CGRect(x: photoImageViewOrigin.x, y: photoImageViewOrigin.y, width: photoDiameter, height: photoDiameter + 20.0)
        self.backgroundColor =  UIColor.clear

        photoFrameView?.backgroundColor = UIColor(red: 182 / 255.0, green: 182 / 255.0, blue: 187 / 255.0, alpha: 1.0)
        //photoFrameView.backgroundColor     = UIColor.gray
        photoFrameView.translatesAutoresizingMaskIntoConstraints = false

        photoFrameView.layer.masksToBounds = true
        photoFrameView.layer.cornerRadius = (photoDiameter + photoFrameViewPadding) / 2
        self.addSubview(photoFrameView!)


        // ----------------
        // Add constraints.
        // ----------------
        //self.setNeedsUpdateConstraints()
        self.setConstraints()
    }



    func setConstraints() {
        //super.updateViewConstraints()
        if didSetupConstraints {
            return
        }
        // ---------------------------
        // The frame of the photo.
        // ---------------------------
        var constraint = NSLayoutConstraint(item: photoFrameView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: (photoDiameter + photoFrameViewPadding))
        photoFrameView?.addConstraint(constraint)
        constraint = NSLayoutConstraint(item: photoFrameView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: (photoDiameter + photoFrameViewPadding))
        photoFrameView?.addConstraint(constraint)
        constraint = NSLayoutConstraint(item: photoFrameView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: 0.0)

        self.addConstraint(constraint)
        constraint = NSLayoutConstraint(item: photoFrameView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0.0)
        self.addConstraint(constraint)


        // ---------------------------
        // The button "add photo".
        // ---------------------------
        constraint = NSLayoutConstraint(item: addPhotoButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: photoDiameter)
        addPhotoButton?.addConstraint(constraint)
        constraint = NSLayoutConstraint(item: addPhotoButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: photoDiameter)
        addPhotoButton?.addConstraint(constraint)
        constraint = NSLayoutConstraint(item: addPhotoButton, attribute: .centerX, relatedBy: .equal, toItem: photoFrameView, attribute: .centerX, multiplier: 1.0, constant: 0.0)
        self.addConstraint(constraint)
        constraint = NSLayoutConstraint(item: addPhotoButton, attribute: .centerY, relatedBy: .equal, toItem: photoFrameView, attribute: .centerY, multiplier: 1.0, constant: 0.0)
        self.addConstraint(constraint)




        // ---------------------------
        // The button "edit photo".
        // ---------------------------
        constraint = NSLayoutConstraint(item: editPhotoButton, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0.0)
        self.addConstraint(constraint)
        constraint = NSLayoutConstraint(item: editPhotoButton, attribute: .top, relatedBy: .equal, toItem: photoFrameView, attribute: .bottom, multiplier: 1.0, constant: -5.0)
        self.addConstraint(constraint)

        didSetupConstraints = true

    }
}

1 个答案:

答案 0 :(得分:0)

你的错误在这里:

profileImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true

profileImageView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true

topAnchor / leadingAnchor.constraint不设置约束,它返回一个可以添加到视图层次结构中的约束。因此,您的profileImageView实际上没有约束。

此外,您没有在约束中仅在框架中设置它的宽度和高度。这是你警告的来源。

<强>解决方案

let leadingConstraint = profileImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor)
let topConstraint = profileImageView.topAnchor.constraint(equalTo: contentView.topAnchor)

//These are just examples, make whichever constraints that you need
let widthConstraint = NSLayoutConstraint(item: profileImageView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 150.0)
let heightConstraint = NSLayoutConstraint(item: profileImageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 150.0)

contentView.addConstraints([leadingConstraint,topConstraint, widthConstraint, heightConstraint])