【发布时间】:2020-12-07 20:50:29
【问题描述】:
我在UIScrollView 中添加了一个Sign in with Apple 按钮,Apple 文档建议的方式;
let signInWithAppleButton = ASAuthorizationAppleIDButton(type: .default, style: .white)
signInWithAppleButton.addTarget(self, action: #selector(loginWithApple(_:)), for: .touchUpInside)
signInWithAppleButton.cornerRadius = 12
scrollView.addSubview(signInWithAppleButton)
问题是,按钮只响应非常长的按下而不是简单的点击。我试过把它放在UIScrollView 之外,它在那里工作!
这是我的UIScrollView 和按钮的设置;
fileprivate func setupScrollViewConstraints() {
scrollView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: self.view.topAnchor),
scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
])
}
func setupSignInWithAppleButton() -> UIControl? {
if #available(iOS 13.0, *) {
let signInWithAppleButton = ASAuthorizationAppleIDButton(type: .default, style: .white)
signInWithAppleButton.addTarget(self, action: #selector(loginWithApple(_:)), for: .touchUpInside)
signInWithAppleButton.cornerRadius = 12
scrollView.addSubview(signInWithAppleButton)
signInWithAppleButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
signInWithAppleButton.topAnchor.constraint(equalTo: accountLabel.bottomAnchor, constant: 8),
signInWithAppleButton.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
signInWithAppleButton.widthAnchor.constraint(equalToConstant: 210),
signInWithAppleButton.heightAnchor.constraint(equalToConstant: 45)
])
return signInWithAppleButton
}
return nil
}
知道是什么破坏了它吗?
编辑;这是我的处理程序;
@objc private func loginWithApple(_ sender: Any) {
let appleIDProvider = ASAuthorizationAppleIDProvider()
let request = appleIDProvider.createRequest()
request.requestedScopes = [.fullName, .email]
let authorizationController = ASAuthorizationController(authorizationRequests: [request])
authorizationController.delegate = self
authorizationController.presentationContextProvider = self
authorizationController.performRequests()
}
我在里面放了一个断点,但是只有长按按钮才会激活!提醒一下,如果我将按钮放在控制器的视图中,它会按预期工作!
【问题讨论】:
-
尝试设置滚动视图的
delaysContentTouches = false。 -
试过了,没用
-
在 iPhone11 / iOS 13.6 上运行时,您的示例的最小实现(我只需要为您的标签假设一个约束 - 以滚动视图为中心)不会重现该问题。触摸按钮后立即发生动作。也许您的层次结构中涉及的任何其他视图导致了问题?您是否尝试过直观地调试视图?
-
我认为这不是 AutoLayout 的问题,因为在
signInWithAppleButton的正下方,我还有 2 个从 Storyboard 创建的按钮,它们工作得很好!你真的创建了一个项目吗?能否请您上传,以便我查看? -
好的,刚刚创建了一个新项目(单视图应用程序模板),然后在视图控制器中did this。
标签: ios uibutton apple-sign-in