【问题标题】:How do I center a UILabel to a UIView programmatically?如何以编程方式将 UILabel 居中到 UIView?
【发布时间】:2020-05-21 06:06:10
【问题描述】:

我已经浏览了这个帖子并且几乎尝试了所有建议,但我的标签仍然拒绝在 view 中居中:

How to center a UILabel on UIView

这是我得到的最接近的:

我的第一个想法是view 隐藏在tab bar 下,但根据debug hierarchyviewtab bar 开始的地方结束。

代码如下:

    _noExperiences = [[UIView alloc] initWithFrame:self.view.frame];
    _noExperiences.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_noExperiences];

    UILabel *nothingToShow = [[UILabel alloc] initWithFrame:CGRectMake(_noExperiences.center.x, _noExperiences.center.y, 200, 20)];
    nothingToShow.text = @"HELLO";
    nothingToShow.textColor = [UIColor blackColor];
    nothingToShow.textAlignment = NSTextAlignmentCenter;
    [nothingToShow setNumberOfLines: 0];
    [nothingToShow sizeToFit];

    [nothingToShow setCenter: CGPointMake(_noExperiences.center.x, _noExperiences.center.y)];
    [nothingToShow setFont:[UIFont fontWithName: @"Trebuchet MS" size: 14.0f]];

    [_noExperiences addSubview:nothingToShow];

【问题讨论】:

  • UILabel *nothingToShow = [[UILabel alloc] initWithFrame:CGRectMake(_noExperiences.frame.size.width/2 - 100, _noExperiences.frame.size.height/2 - 10, 200, 20)] ;
  • 您可以使用 Autolayout,您可以使用自动调整大小的掩码,您可以使用 PinLayout,您可以使用 LayoutKit 等,您可以在 viewWillLayoutSubviews 中手动布局标签,或者您可以在 root 中创建自定义视图并在layoutSubviews 方法。有很多选择。你更喜欢哪个?

标签: ios objective-c uiview uilabel centering


【解决方案1】:

你会很多更好地学习/使用约束和自动布局。

这是您正在尝试执行的操作,但它会自动布局适用于所有不同的设备和旋转:

_noExperiences = [UIView new];
_noExperiences.backgroundColor = [UIColor whiteColor];

UILabel *nothingToShow = [UILabel new];
nothingToShow.text = @"HELLO";
nothingToShow.textColor = [UIColor blackColor];
nothingToShow.textAlignment = NSTextAlignmentCenter;
[nothingToShow setNumberOfLines: 0];
[nothingToShow setFont:[UIFont fontWithName: @"Trebuchet MS" size: 14.0f]];

// add _noExperiences to self.view
[self.view addSubview:_noExperiences];

// add nothingToShow to _noExperiences
[_noExperiences addSubview:nothingToShow];

// we'll use auto-layout, so set to NO
_noExperiences.translatesAutoresizingMaskIntoConstraints = NO;
nothingToShow.translatesAutoresizingMaskIntoConstraints = NO;

// let's respect safe area
UILayoutGuide *g = self.view.safeAreaLayoutGuide;

[NSLayoutConstraint activateConstraints:@[

    // constrain _noExperiences view to safe area
    [_noExperiences.topAnchor constraintEqualToAnchor:g.topAnchor constant:0.0],
    [_noExperiences.bottomAnchor constraintEqualToAnchor:g.bottomAnchor constant:0.0],
    [_noExperiences.leadingAnchor constraintEqualToAnchor:g.leadingAnchor constant:0.0],
    [_noExperiences.trailingAnchor constraintEqualToAnchor:g.trailingAnchor constant:0.0],

    // constrain nothingToShow label centered in _noExperiences view
    [nothingToShow.centerXAnchor constraintEqualToAnchor:_noExperiences.centerXAnchor],
    [nothingToShow.centerYAnchor constraintEqualToAnchor:_noExperiences.centerYAnchor],

]];

编辑

如果你真的需要支持那 7 位使用早于 11 的 iOS 版本的用户,你可以试试这个来处理丢失的safeAreaLayoutGuide

// let's respect safe area for iOS 11+
// or layoutMarginsGuide for earlier
UILayoutGuide *g;
CGFloat standardSpacing = 0.0;

if (@available(iOS 11, *)) {
    // iOS 11 (or newer) ObjC code
    g = self.view.safeAreaLayoutGuide;
    [NSLayoutConstraint activateConstraints:@[
        // constrain _noExperiences view to safe area
        [_noExperiences.topAnchor constraintEqualToAnchor:g.topAnchor constant:0.0],
        [_noExperiences.bottomAnchor constraintEqualToAnchor:g.bottomAnchor constant:0.0],
        [_noExperiences.leadingAnchor constraintEqualToAnchor:g.leadingAnchor constant:0.0],
        [_noExperiences.trailingAnchor constraintEqualToAnchor:g.trailingAnchor constant:0.0],
    ]];
} else {
    // iOS 10 or older code
    standardSpacing = 8.0;
    g = self.view.layoutMarginsGuide;
    [NSLayoutConstraint activateConstraints:@[
        // constrain _noExperiences view to layout margins guide
        [_noExperiences.topAnchor constraintEqualToAnchor:g.topAnchor constant:standardSpacing],
        [_noExperiences.bottomAnchor constraintEqualToAnchor:g.bottomAnchor constant:-standardSpacing],
        [_noExperiences.leadingAnchor constraintEqualToAnchor:g.leadingAnchor constant:0.0],
        [_noExperiences.trailingAnchor constraintEqualToAnchor:g.trailingAnchor constant:0.0],
    ]];
}

[NSLayoutConstraint activateConstraints:@[

    // constrain nothingToShow label centered in _noExperiences view
    [nothingToShow.centerXAnchor constraintEqualToAnchor:_noExperiences.centerXAnchor],
    [nothingToShow.centerYAnchor constraintEqualToAnchor:_noExperiences.centerYAnchor],

]];

【讨论】:

  • 抱歉回复晚了,谢谢!效果很好!在我尝试过的所有事情中,这是唯一真正有效的事情。不过有一件事。 safeAreaLayoutGuide 仅适用于 iOS 11 及更高版本。尝试跳过它,但结果一团糟。对于 iOS
【解决方案2】:

请尽快尝试:

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
        label.center = view.center
        label.textAlignment = .center
        label.text = "label"
        self.view.addSubview(label)

【讨论】:

    猜你喜欢
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多