我在iOS中使用FBSDKLoginKit
和Swift。
直到最近它一直工作得很好,但我现在无法覆盖故事板中按钮的高度?
由于某些原因,按钮的高度现在要小得多。我已经尝试为按钮设置高度约束,将按钮放在堆栈视图中并设置为按比例填充,甚至覆盖SDK中的按钮高度,没有运气。
如果我将按钮更改为正常的UIButton
,则布局约束可以完美地工作。
这是我运行应用程序时按钮的样子。
这就是我希望按钮看起来的方式 - 大小明智。
我也遇到过这个问题。其原因在4.18.0
to 4.19.0
upgrade guide中解释:
FBSDKLoginButton
UI在4.19.0中已更改。该按钮现在显示“继续使用Facebook”,而不是“使用Facebook登录”。按钮颜色从#3B5998更改为#4267B2。由于使用较小的字体大小和较大的Facebook徽标周围的填充,按钮高度现在固定为28。
到目前为止,我发现的唯一解决方法是将SDK版本降级为4.18.0
(它为我完成了工作)。
在未来的SDK更新中,FB可能会解决这个问题(......他们为很多人创建了这个问题)。
为了获得更持久的解决方案,我们可以看到specific changes that caused this, on GitHub。我发现最可疑的变化始于line 194
:
[self addConstraint:[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:28]];
如果删除/禁用上述约束,则可以帮助扭转这种情况。它应该看起来像这样(我在撰写本文时手头还没有IDE):
// Obtain all constraints for the button:
let layoutConstraintsArr = fbLoginButton.constraints
// Iterate over array and test constraints until we find the correct one:
for lc in layoutConstraintsArr { // or attribute is NSLayoutAttributeHeight etc.
if ( lc.constant == 28 ){
// Then disable it...
lc.active = false
break
}
}
当我有机会测试上述内容或者如果我找到更好的解决方案时,我会更新答案。
Autolayout不再适用于Facebook按钮(FBSDKButton)。我用按钮框改变了它的高度。
let fbLoginbutton = FBSDKLoginButton()
view.addSubview(fbLoginbutton)
fbLoginbutton.frame = CGRect(x: 38, y: 397, width: 300, height: 38)
您可以根据自己的要求进行设置。虽然我仍然无法更改其字体和徽标大小。
如果您只是在更改按钮的高度后,只需在Storyboard中添加按钮后调整按钮上已存在的高度约束的常量:
for constraint in facebookButton.constraints where constraint.firstAttribute == .height {
constraint.constant = YOUR_Height
}
此代码可以放在viewDidLoad()
中。
您只需简单地覆盖facebook按钮即可轻松实现此目的。
迅速:
class FacebookButton: FBSDKLoginButton {
override func updateConstraints() {
// deactivate height constraints added by the facebook sdk (we'll force our own instrinsic height)
for contraint in constraints {
if contraint.firstAttribute == .height, contraint.constant < standardButtonHeight {
// deactivate this constraint
contraint.isActive = false
}
}
super.updateConstraints()
}
override var intrinsicContentSize: CGSize {
return CGSize(width: UIViewNoIntrinsicMetric, height: standardButtonHeight)
}
override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
let logoSize: CGFloat = 24.0
let centerY = contentRect.midY
let y: CGFloat = centerY - (logoSize / 2.0)
return CGRect(x: y, y: y, width: logoSize, height: logoSize)
}
override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
if isHidden || bounds.isEmpty {
return .zero
}
let imageRect = self.imageRect(forContentRect: contentRect)
let titleX = imageRect.maxX
let titleRect = CGRect(x: titleX, y: 0, width: contentRect.width - titleX - titleX, height: contentRect.height)
return titleRect
}
}
在此代码示例中,standardButtonHeight
是具有所需按钮高度的已定义常量。
另请注意,24.0
的徽标大小与SDK 4.18版中使用的大小相同。
所以我采用了@ Dev-iL的解决方案,并将其调整为更具未来性的证据。我对此非常陌生,所以我花了几个小时来搞清楚,但我想我会分享,因为它基于高度约束而不是基于常量值来专门停用高度约束。
我在故事板中使用了一个归类为Facebook按钮的子视图,并在那里设置了新约束。
我更喜欢这种方法,并感觉它更清洁。
注意:我相信高度限制它始终是第一个值但是如果我错了请更正我,我会用编辑更新。正如我所提到的,我是新手
编辑:我决定包含常量值28以允许在删除期间跳过我的故事板高度约束。如果在删除后以编程方式添加约束,则不需要这样做
for const in fbLoginButton.constraints{
if const.firstAttribute == NSLayoutAttribute.height && const.constant == 28{
fbLoginButton.removeConstraint(const)
}
}
至于现在,Facebook按钮只有一个约束,即高度约束,你可以删除按钮的所有约束并添加你的。
facebookSignInButton.removeConstraints(facebookSignInButton.constraints)
但是,当然这可能在将来发生变化,您可能会删除您不想要的约束。如果你只删除那个有问题的约束,也许更好的解决方案。
if let facebookButtonHeightConstraint = facebookSignInButton.constraints.first(where: { $0.firstAttribute == .height }) {
facebookSignInButton.removeConstraint(facebookButtonHeightConstraint)
}
作为最后的手段,尝试实现自己的自定义按钮作为Facebook登录按钮。他们可能会阻止SDK中的按钮自定义。 https://developers.facebook.com/docs/swift/login - 这里有一节带有示例代码 - “自定义登录按钮”。它似乎并不复杂。
我们遇到了同样的问题。我们通过使用initWithFrame
方法在代码中创建按钮来解决此问题。
来自文档
FBSDKLoginButton
的固定高度为@c 30像素,但您可以更改宽度。initWithFrame:CGRectZero
会将按钮调整到最小框架。
这个解决方案对我们有用
let facebookButton = FBSDKLoginButton(frame:facebookButtonPlaceholder.bounds)
facebookButton.readPermissions = ["email"]
facebookButton.backgroundColor = UIColor.clear
facebookButtonPlaceholder.addSubview(facebookButton)
facebookButtonPlaceholder.backgroundColor = UIColor.clear
我可以设法通过这种方式改变按钮的高度:
let loginButton = LoginButton(frame: self.facebookButtonView.frame, readPermissions: [ .publicProfile ])
self.view.addSubview(loginButton)
Facebook按钮的大小与facebookButtonView相同。我测试了高度50,它正在工作。
意识到这是一个老问题,我找到了一个解决方案,并将其作为未来参考的答案发布。
通过Pod安装FBSDKLoginKit后,查看XCODE左侧的目录并打开您的PODS - 而不是您的Podfile。打开FBSDKLoginKit,打开FBSDKLoginButton.m文件。
您现在将看到一条警告,指示您正在编辑。忽略警报,注意消息,并确保不更改目标信息以外的任何内容。更改在文件中指定Facebook按钮高度的两个字段,如下所示:
图片可以帮助您完成上述指南:
EASIEST SOLUTION,无需处理程序化的需求,只需在故事板中完成
如果您没有理由删除约束,只需覆盖它,Swift 4.0中的这个小autoclosure也可以正常工作。
let facebookLoginButton = FBSDKLoginButton()
if let constraint = facebookLoginButton.constraints.first(where: { (constraint) -> Bool in
return constraint.firstAttribute == .height
}) {
constraint.constant = 40.0
}
或者,如果你讨厌let语句:
let facebookLoginButton = FBSDKLoginButton()
facebookLoginButton.constraints.first(where: { (constraint) -> Bool in
return constraint.firstAttribute == .height
})?.constant = 40.0