无法设置自定义视图的限制,斯威夫特

问题描述:

我不能设置领导和主要约束的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 

    } 
} 

你的错误是在这里:

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

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

topAnchor/leadingAnchor.constraint不设置约束,它返回可以添加到视图hiera中的约束rchy。因此,您的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])