代码之家  ›  专栏  ›  技术社区  ›  BostonMacOSX

Facebook按钮对齐问题

  •  0
  • BostonMacOSX  · 技术社区  · 8 年前

    所以我要补充

    Problem alignment

      var FBlogBut = FBSDKLoginButton()
        loginView.addSubview(FBlogBut)
    
    
        NSLayoutConstraint(item: FBlogBut, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 350 ).isActive = true
    
    
        NSLayoutConstraint(item: FBlogBut, attribute: .centerX, relatedBy: .equal, toItem: loginView, attribute: .centerX, multiplier: 1, constant: 0 ).isActive = true
    
        loginView.layoutIfNeeded()
    

    所以我不知道为什么按钮在左上角,而所有其他项目都正常。。它至少应该是350和cetnered。

    1 回复  |  直到 7 年前
        1
  •  1
  •   EmilioPelaez    8 年前

    有两个原因。

    首先,您只需设置中心x和宽度。您需要指定垂直约束,即顶部约束或中心约束。

    第二个是默认情况下 translatesAutoresizingMaskIntoConstraints 将 true ,这意味着视图将其原点和大小设置为约束。这会弄乱你的布局。

    只需将其设置为false: FBlogBut.translatesAutoresizingMaskIntoConstraints = false

    注:如果你想遵循命名约定,变量名是大小写的,这意味着第一个字符是小写的。在这种情况下,不需要缩写,按钮名称可以是 fbLoginButton 而且不会太长。 FBLogBut 不清楚这是登录还是注销按钮。