vendredi 8 mai 2015

Center UILabel Subview

I have a UIImageView as the background view of my table view. When there is no data for the table, I am adding a UILabel as a subview of the image view. I am having trouble getting the label centered though. The label is always off to the right.

UIImageView *backgroundImageView = [[UIImageView alloc]initWithFrame:self.tableView.frame];
    backgroundImageView.image = [UIImage imageNamed:@"background"];
    backgroundImageView.center = self.tableView.center;

    self.noLocationsLabel = [[UILabel alloc]initWithFrame:backgroundImageView.frame];
    self.noLocationsLabel.center = backgroundImageView.center;
    self.noLocationsLabel.text = @"No saved locations";
    self.noLocationsLabel.textAlignment = NSTextAlignmentCenter;
    self.noLocationsLabel.textColor = [UIColor blackColor];
    self.noLocationsLabel.font = [UIFont fontWithName:@"Chalkboard SE" size:24.0];

self.tableView.backgroundView = backgroundImageView;

if (self.locationsArray.count == 0) {
        self.navigationItem.rightBarButtonItem.enabled = NO;

        [self.tableView.backgroundView addSubview:self.noLocationsLabel];
        self.tableView.userInteractionEnabled = NO;
    }

Aucun commentaire:

Enregistrer un commentaire