Programmatically Add CenterX/CenterY Constraints

前端 未结 7 742

I have a UITableViewController that doesn\'t display any sections if there is nothing to show. I\'ve added a label to indicate to the user that there is nothing to display w

7条回答
  •  借酒劲吻你
    2020-11-29 18:53

    A solution for me was to create a UILabel and add it to the UIButton as a subview. Finally I added a constraint to center it within the button.

    UILabel * myTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 75, 75)];
    myTextLabel.text = @"Some Text";
    myTextLabel.translatesAutoresizingMaskIntoConstraints = false;
    
    [myButton addSubView:myTextLabel];
    
    // Add Constraints
    [[myTextLabel centerYAnchor] constraintEqualToAnchor:myButton.centerYAnchor].active = true;
    [[myTextLabel centerXAnchor] constraintEqualToAnchor:myButton.centerXAnchor].active = true; 
    

提交回复
热议问题