gpt4 book ai didi

ios - fb 登录按钮未在 View 中正确显示

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

我有以下代码:

override func viewDidLoad() {
super.viewDidLoad()

let fbLoginButton = FBSDKLoginButton()
fbLoginView.contentMode = UIViewContentMode.scaleAspectFit
fbLoginView.addSubview(fbLoginButton)
fbLoginButton.frame.origin = fbLoginView.frame.origin

fbLoginButton.frame = CGRect(x: fbLoginView.frame.origin.x, y: fbLoginView.frame.origin.y, width: fbLoginView.frame.width, height: fbLoginView.frame.height)

print("INFO width: \(fbLoginView.frame.width) height: \(fbLoginView.frame.height)")
print("INFO width: \(fbLoginButton.frame.width) height: \(fbLoginButton.frame.height)")
// fbLoginButton.delegate = self
}

我正在尝试在主视图中的一个小 subview 中添加 facebook 登录按钮。出于某种原因,当我打印出 UIView 的宽度/高度和 facebook 登录按钮时,我看到了这个:

信息宽度:1000.0 高度:1000.0INFO 宽度:1000.0 高度:1000.0

但是当我在模拟器上运行该应用程序时,我看到 View 不是 1000.0 x 1000.0(它会溢出屏幕但事实并非如此。它看起来像这样:

enter image description here

您可以看到按钮的蓝色,并且它正确地位于较小的 subview 中,但是按钮实际上似乎比 View 溢出。我不明白为什么 View 在模拟器中的大小是正确的,但它说它的大小太大了。我读过这里的帖子,告诉我调整按钮的大小以适合纵横比,我试过所有其他选项但没有运气。

我将较小的 uiview 设置如下: enter image description here

最佳答案

viewDidLoad 中,应用尚未完成其自动布局过程,因此很多 View 都是 1000x1000pts。

因此在您的代码中,当您将 loginButton 的大小设置为与 loginView 的大小匹配时,它会复制 1000 的值穿过。

稍后,当您的应用应用其自动布局约束时,包含的 loginView 将调整为正确的大小,但其中的 loginButton 具有旧的、更大的尺寸。

正确的解决方案是使用自动布局正确约束 loginButton - 最简单的更改是在 loginView 中对其添加约束,以便同时调整其大小:

NSLayoutConstraint(item: fbLoginButton, attribute: .Trailing, relatedBy: .Equal, toItem: fbLoginView, attribute: .Trailing, multiplier: 1.0, constant: 0.0).active = true
NSLayoutConstraint(item: fbLoginButton, attribute: .Leading, relatedBy: .Equal, toItem: fbLoginView, attribute: .Leading, multiplier: 1.0, constant: 0.0).active = true
NSLayoutConstraint(item: fbLoginButton, attribute: .Top, relatedBy: .Equal, toItem: fbLoginView, attribute: .Top, multiplier: 1.0, constant: 0.0).active = true
NSLayoutConstraint(item: fbLoginButton, attribute: .Bottom, relatedBy: .Equal, toItem: fbLoginView, attribute: .Bottom, multiplier: 1.0, constant: 0.0).active = true

或者直接在 Storyboard中添加 Facebook 按钮可能会更清晰(添加一个空 View 并将其类设置为 FBSDKLoginButton)。

关于ios - fb 登录按钮未在 View 中正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41680372/

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