如何以编程方式将 UILabel 置于 UIView 的中心?

How do I center a UILabel to a UIView programmatically?

我已经完成了这个线程并且几乎尝试了每一个建议,但我的标签仍然拒绝在 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];

请尝试 swift :

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)

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

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

_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],

]];

编辑

如果您确实需要支持使用iOS 11 之前版本的这 7 位用户,您可以试试这个来处理缺少的 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],

]];