swift - 无法为自定义 View 设置约束,Swift

标签 swift uitableview nslayoutconstraint

我无法为 tableViewCell 内的自定义 View 设置前导和顶部约束,我还收到一条错误消息,其中显示

size is ambiguous for profileImageView

但是,我能够将 profileImageView 居中到单元格的中心,但是当我尝试设置leadingAnchor(如下面的函数setUp所示)时,将使用 View 的中心而不是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 不设置约束,它返回一个可以添加到 View 层次结构中的约束。因此,您的 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])

关于swift - 无法为自定义 View 设置约束,Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46399570/

相关文章:

ios - 使用 UIBezierPath :byRoundingCorners: with Swift 2 and Swift 3

swift - 如何在 Swift 4 中增加 Index.String 类型的变量?

ios - 如果在重新加载 UiTableView 时 View 消失,应用程序会崩溃

ios - 如何管理 iOS 中可移动 View 的约束?

swift - 算术表达式中的字面 Int 到 Double 强制转换

ios - 如何在 ios UITextfield 中显示完整的占位符文本?

ios - 以编程方式向已以编程方式添加到表格单元格的标签添加约束

ios - swift - 按类别/标签在表格 View 中显示项目?

ios - 在运行时更改 AutoLayout 约束

ios - Xcode 7 : Autolayout to UICollectionViewCell that are generated programmatically