gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 12:18:32 25 4
gpt4 key购买 nike

我见过类似的 SO 问题,最有前途的问题建议设置 .isUserInteractionEnabled = true.bringSubview(toFront: subView) 但是这两个都没有似乎适用于我的情况(不确定我是否正确使用它)。我无法为 View 设置背景颜色和边框。

请指出我哪里出错了。

我想在点击 subview 内的 editButton 时调用一个函数。

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

enter image description here

代码如下:

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: "")
}


}

ViewController 使用 ProfilePhotoView

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() 来激活约束而不是将它们添加到 View 中。 iOS 将为您将约束添加到正确的 View 。此外,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)
)

关于ios - UIView 子类中的 UIButton 不起作用,Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45625227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com