简体   繁体   中英

How do I center a UILabel to a UIView programmatically?

I have gone through this thread and pretty much tried every suggestion, but still my label refuse to center in the view :

How to center a UILabel on UIView

This is the closest I get:

在此处输入图片说明

My first thought was that the view hides under the tab bar but according the the debug hierarchy the view ends right where the tab bar starts.

The code looks like this:

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

You will be much better off learning / using constraints and auto-layout.

Here is what you are trying to do, but it will auto-layout correctly for all different devices and rotations:

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

]];

EDIT

If you really need to support those 7 users who are using an iOS version earlier than 11, you can try this to handle the missing 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],

]];

please try for 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM