ios - swift - xcode11 中使用 frame = bound 选项时未对齐
问题描述
我是 iPhone 开发的初学者。我正在修改现有项目中的一些代码。
我在注册时有一个签名按钮,当它对齐良好时。约束设置正确。但现在我在我的登录页面中添加了相同的代码(Apple 登录按钮代码)。但它没有很好地居中和对齐。下面是我用于添加苹果登录按钮的代码
if #available(iOS 13.0, *) {
let authorizationButton = ASAuthorizationAppleIDButton()
authorizationButton.addTarget(self, action: #selector(handleLogInWithAppleIDButtonPress), for: .touchUpInside)
authorizationButton.frame = self.appleSignInButton.bounds
self.appleSignInButton.addSubview(authorizationButton)
} else {
self.appleSignInView.isHidden = true
}
两个页面的约束属性也相同。
解决方案
对齐视图时不要打扰frame
s 和。bounds
让Auto Layout通过提供适当的约束来处理它:
let authorizationButton = ASAuthorizationAppleIDButton()
authorizationButton.addTarget(self, action: #selector(handleLogInWithAppleIDButtonPress), for: .touchUpInside)
self.appleSignInButton.addSubview(authorizationButton)
// Don't use the frame, place the view using constraints
authorizationButton.translatesAutoresizingMaskIntoConstraints = false
// Set the constraints after adding the view to the view hierarchy
NSLayoutConstraint.activate([
authorizationButton.leadingAnchor.constraint(equalTo: appleSignInButton.leadingAnchor),
authorizationButton.trailingAnchor.constraint(equalTo: appleSignInButton.trailingAnchor),
authorizationButton.topAnchor.constraint(equalTo: appleSignInButton.topAnchor),
authorizationButton.bottomAnchor.constraint(equalTo: appleSignInButton.bottomAnchor)
])
推荐阅读
- unit-testing - 在单元测试中在 .net Core Web API 控制器上设置路由数据值
- javascript - 使用 (fa-awesome) 开始一个 css 动画并以另一个图标结束
- javascript - 简单的 React-multi-carousel 示例失败
- mysql - 将 ROLLUP 与 COUNT DISTINCT 一起使用
- python - 有没有一种巧妙的方法来折叠 Python 中的 3 个 for 循环
- c# - 如何判断 PDF 文本是否已旋转
- r - 为列表收集 colname
- rust - 如何将可见性令牌传递给位标志!宏?
- c# - 从带有数据的 Razor 页面重定向
- c++ - 我们如何在 Github Workflow 中运行 C++ 单元测试?