繁体   English   中英

UIView子类中的UIButton不起作用,Swift

[英]UIButton inside a UIView subclass is not working, Swift

我见过类似的SO问题,最有前途的问题建议设置.isUserInteractionEnabled = true.bringSubview(toFront: subView)但是这两种方法在我看来都不起作用(不确定我是否使用正确)。 我既不能为视图设置backgroundColor和border。

请告诉我我要去哪里错了。

我想在子视图中单击editButton时调用一个函数。

这是我运行代码时得到的:

在此处输入图片说明

这是代码:

class ProfilePhotoView: UIView{

    var profileImage    =   UIImageView()
    var editButton      =   UIButton()
    var currentViewController   : UIViewController



    init(frame: CGRect, viewController : UIViewController){
        self.currentViewController = viewController

        super.init(frame: frame)
        self.isUserInteractionEnabled = true
        setup()
    }



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


    func setup(){

        profileImage.image = #imageLiteral(resourceName: "profilePlaceHolder")
        editButton.setTitle("edit", for: .normal)
        editButton.setTitleColor(Colors.curieBlue, for: .normal)
        editButton.isUserInteractionEnabled = true
        editButton.addTarget(self, action: #selector(editPhoto), for: .touchUpInside)

        profileImage.translatesAutoresizingMaskIntoConstraints  =   false

        editButton.translatesAutoresizingMaskIntoConstraints     =   false

        self.addSubview(profileImage)
        self.addSubview(editButton)


        let viewsDict = [ "profileImage"    :   profileImage,
                          "editButton"       :   editButton
        ] as [String : Any]

        self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[profileImage]", options: [], metrics: nil, views: viewsDict))
        self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-10-[profileImage]", options: [], metrics: nil, views: viewsDict))



        self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[editButton]", options: [], metrics: nil, views: viewsDict))
        self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[profileImage]-10-[editButton]", options: [], metrics: nil, views: viewsDict))

    }

    func editPhoto(){
        Utils.showSimpleAlertOnVC(targetVC: currentViewController, title: "Edit Button Clicked", message: "")
    }


}

使用ProfilePhotoView的ViewController

class ProfilePhotoViewVC: UIViewController {

    var profilePhotoView    :   ProfilePhotoView!
    //let imagePickerController   =   UIImagePickerController() // Initializing imagePicker
    //var imagePickerDelegate     =   ProfilePhotoDelegate()



    override func viewDidLoad() {
        super.viewDidLoad()

        let profilePhotoFrame               = CGRect(x: 0, y: 0, width: 300, height: 800)
        profilePhotoView                    =   ProfilePhotoView(frame: profilePhotoFrame, viewController: self)
        profilePhotoView.isOpaque           = false
        profilePhotoView.backgroundColor    = Colors.lightGrey
        profilePhotoView.layer.borderWidth  = 1
        profilePhotoView.layer.borderColor  = Colors.iOSBlue.cgColor
        profilePhotoView.translatesAutoresizingMaskIntoConstraints   =   false

        view.addSubview(profilePhotoView)
        view.bringSubview(toFront: profilePhotoView)

        view.isUserInteractionEnabled = true

        let viewsDict = [ "profilePhotoView"    :   profilePhotoView] as [String : Any]

        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[profilePhotoView]", options: [], metrics: nil, views: viewsDict))
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-10-[profilePhotoView]", options: [], metrics: nil, views: viewsDict))

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

设置此设置后:

profilePhotoView.translatesAutoresizingMaskIntoConstraints = false

您的profilePhotoViewwidthheight设置为0 ,并且需要使用约束条件对其进行指定。 在您的视觉格式中添加(==300)(==800)以进行设置:

view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[profilePhotoView(==300)]", options: [], metrics: nil, views: viewsDict))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-10-[profilePhotoView(==800)]", options: [], metrics: nil, views: viewsDict))

另外,考虑使用NSLayoutConstraint.activate()激活约束,而不是将约束添加到视图中。 iOS将为您添加约束到正确的视图。 另外, options的默认值为[] ,因此可以将其保留为关闭状态:

NSLayoutConstraint.activate(NSLayoutConstraint.constraints(
    withVisualFormat: "H:|-10-[profilePhotoView(==300)]",
    metrics: nil, views: viewsDict)
)
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(
    withVisualFormat: "V:|-10-[profilePhotoView(==800)]",
    metrics: nil, views: viewsDict)
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM