首页 文章

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

提问于
浏览
0

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

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

运行应用程序时,该按钮不显示 Headers . 如果我在使用上述方法后按NS按钮的 Headers ,它实际上已被设置为我传入的 Headers .

这是我的代码 . 这是在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]];

1 回答

  • 1

    设置按钮 Headers 的代码是正确的,因此请使用此代码设置 title color

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

相关问题