UIButton 的标题属性记录正确,但在将按钮添加为子视图时未显示



我已经创建了一个UIButton,我可以成功地将其添加为子视图并在屏幕上看到它。问题是,当我尝试使用:

[myButton setTitle:@"My Title" forState:UIControlStateNormal];

当我运行应用程序时,该按钮不会显示标题。如果我在使用上述方法后NSLog按钮的标题,它实际上已经被设置为我传递的标题

这是我的密码。这在UIView子类中。我意识到其中一些逻辑可能不属于UIView子类,但我只是想让它尽可能快地工作:

// Set the view's frame, background color, corner radius
self.frame = CGRectMake(45, 200, 235, 187);
self.backgroundColor = [UIColor whiteColor];
self.layer.borderWidth = 1;
self.layer.borderColor = [UIColor grayColor].CGColor;
self.layer.cornerRadius = 5;
// Create the popup's body text label
self.popupBodyTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 150)];
self.popupBodyTextLabel.text = @"Text goes here.";
// Add text view to popup's subviews
[self addSubview:self.popupBodyTextLabel];
// Create ok button
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 143, 120, 44)];
myButton.backgroundColor = [UIColor whiteColor];
myButton.layer.borderWidth = 1;
myButton.layer.borderColor = [UIColor grayColor].CGColor;
[myButton setTitle:@"OK" forState:UIControlStateNormal];

// Round the button's bottom left corner
UIBezierPath *myButtonMaskPath = [UIBezierPath bezierPathWithRoundedRect:myButton.bounds byRoundingCorners:(UIRectCornerBottomLeft) cornerRadii:CGSizeMake(5.0, 5.0)];
CAShapeLayer *okButtonMaskLayer = [[CAShapeLayer alloc] init];
myButtonMaskLayer.frame = myButton.bounds;
myButtonMaskLayer.path = myButtonMaskPath.CGPath;
myButton.layer.mask = myButtonMaskLayer;

// Add selector to button
[myButton addTarget:self action:@selector(myButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
// Add button to subviews
[self addSubview:myButton];
// Create the background view
self.backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
self.backgroundView.backgroundColor = [UIColor grayColor];
self.backgroundView.alpha = 0.5f;
// Show our new views
[[[UIApplication sharedApplication] keyWindow] addSubview:self.backgroundView];
[[[UIApplication sharedApplication] keyWindow] addSubview:[MPPopupView shared]];

设置按钮标题的代码是正确的,因此使用此代码设置标题颜色

   [myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

最新更新