【问题标题】:Center a UIImageView and a UILabel together in a UIView在 UIView 中将 UIImageView 和 UILabel 居中
【发布时间】:2015-05-07 20:21:06
【问题描述】:

如果我声明一个 UILabel 和一个不同大小的 UIImageView ,我可以将它们居中在一个 UIView 中,两个水平之间有 10.0points 吗?

谢谢!

【问题讨论】:

    标签: ios uiview uiimageview uibutton uilabel


    【解决方案1】:

    这可以使用自动布局来完成。 您可以将label的右边距设置为superview CenterX左侧5点,imageview左边距设置为superview CenterX右侧5点。

    【讨论】:

      【解决方案2】:

      是的,你可以。基本上你需要的是适当的约束。您可以从代码或故事板中添加约束

      代码示例:

      NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterX multiplier:1 constant:5];
      [view addConstraint:constraint];
      

      NSLayoutConstraint Documentation

      Auto Layout Tutorial

      Adaptive Layout Tutorial

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-28
        • 2016-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多