Ios UIButton选择器功能异常
在我的应用程序中,我有一个UIButton,在上面运行得非常好Ios UIButton选择器功能异常,ios,swift,Ios,Swift,在我的应用程序中,我有一个UIButton,在上面运行得非常好 XCode模拟器(主要使用iOS 11.4和iOS 14.5) 物理设备(iPhone 6:iOS 12.5、iPhone 7:iOS 14.5、iPhone 12:iOS 14.5) 我的问题是,我客户的一些电话没有按预期工作 iPhone XR:iOS 14.5 iphonexsmax:ios14.5 iPhone8 Plus:iOS 14.4 预期行为- 用户单击按钮>调用选择器函数>触发回调>转到另一个视图控制器 c
- XCode模拟器(主要使用iOS 11.4和iOS 14.5)
- 物理设备(iPhone 6:iOS 12.5、iPhone 7:iOS 14.5、iPhone 12:iOS 14.5)
- iPhone XR:iOS 14.5
- iphonexsmax:ios14.5
- iPhone8 Plus:iOS 14.4
class MainViewController: UIViewController {
private let containerView: UIView = {
let view = UIView(frame: .zero)
return view
}()
private var currentContentVC: UIViewController
init() {
let childVC = ChildViewController()
currentContentVC = childVC
childVC.delegate = self
super.init(nibName: nil, bundle: nil)
}
override func loadView() {
super.loadView()
view.addSubview(containerView);
containerView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
containerView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
containerView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
currentContentVC.view.frame = containerView.bounds
addChild(currentContentVC)
containerView.addSubview(currentContentVC.view)
}
}
extension MainViewController: ChildViewControllerCallBack {
func btnPressed() {
let oldVC = currentContentVC
let newVC = OtherChildViewController()
newVC.delegate = self
newVC.view.frame = containerView.bounds
currentContentVC = newVC
switchChildController(from: oldVC, to: currentContentVC, options: options)
}
}
客户的电话行为-
用户点击了按钮,什么也没发生。我很确定这个应用程序已经捕捉到了触摸,因为按下的按钮上有阴影
下面是我使用的示例代码
MainViewController:此控制器的目的是在不同的子视图控制器之间交换
class MainViewController: UIViewController {
private let containerView: UIView = {
let view = UIView(frame: .zero)
return view
}()
private var currentContentVC: UIViewController
init() {
let childVC = ChildViewController()
currentContentVC = childVC
childVC.delegate = self
super.init(nibName: nil, bundle: nil)
}
override func loadView() {
super.loadView()
view.addSubview(containerView);
containerView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
containerView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
containerView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
currentContentVC.view.frame = containerView.bounds
addChild(currentContentVC)
containerView.addSubview(currentContentVC.view)
}
}
extension MainViewController: ChildViewControllerCallBack {
func btnPressed() {
let oldVC = currentContentVC
let newVC = OtherChildViewController()
newVC.delegate = self
newVC.view.frame = containerView.bounds
currentContentVC = newVC
switchChildController(from: oldVC, to: currentContentVC, options: options)
}
}
ChildViewController:由故障按钮组成的控制器
class ChildViewController: UIViewController {
weak var delegate: ChildViewControllerCallBack?
private let mainV: UIView = {
let view = UIView()
return view
}()
private let detailV: UIView = {
let view = UIView()
return view
}()
private let myBtn: UIButton = {
let button = UIButton(type: .custom)
let buttonImage = UIImage(named: "btn_gold_square")
let imageRatio = (buttonImage?.size.width)! / (buttonImage?.size.height)!
button.setBackgroundImage(buttonImage, for: .normal)
button.titleLabel?.font = Font.dynamicXSmallFont
button.titleLabel?.numberOfLines = 2
button.titleLabel?.textAlignment = .center
button.setTitle(title, for: .normal)
button.setTitleColor(Colors.black, for: .normal)
let imageWidth = (SharedData.shared().dialogWidth + (SharedData.shared().dialogWidth * 0.1)) * 0.25
let imageHeight = imageWidth / imageRatio
button.widthAnchor.constraint(equalToConstant: imageWidth).isActive = true
button.heightAnchor.constraint(equalToConstant: imageHeight).isActive = true
button.addTarget(self, action: #selector(btnPressed), for: .touchUpInside)
return button
}()
protocol hildViewControllerCallBack: class {
func btnPressed()
}
override func loadView() {
super.loadView()
view.addSubview(mainV);
mainV.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
mainV.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
mainV.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
mainV.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
[detailV, myBtn].forEach { mainV.addSubview(0) }
detailV.topAnchor.constraint(equalTo: mainV.topAnchor, constant: 0).isActive = true
detailV.leadingAnchor.constraint(equalTo: mainV.leadingAnchor, constant: 0).isActive = true
detailV.bottomAnchor.constraint(equalTo: mainV.bottomAnchor, constant: 0).isActive = true
detailV.widthAnchor.constraint(equalTo: mainV.widthAnchor, multiplier: 0.7).isActive = true
detailV.centerYAnchor.constraint(equalTo: mainV.centerYAnchor).isActive = true
myBtn.leadingAnchor.constraint(equalTo: detailV.trailingAnchor, constant: 0).isActive = true
myBtn.trailingAnchor.constraint(equalTo: mainV.trailingAnchor, constant: 0).isActive = true
}
@objc private func btnPressed(){
delegate?.btnPressed()
}
}
在MainViewController类中的addChild
addChild(currentContentVC)
之后添加此行currentContentVC.didMove(toParent:self)
。您是否在控制台日志中看到有关约束的任何警告?我遇到了一个关于约束的问题,它使一些功能无法工作