0

在我的 UIView 子类(我在 Interface Builder 中实例化)中,有几个按钮作为子视图,我在 initWithCoder 方法中添加到我的视图中,如下所示:

theButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [theButton setFrame:CGRectMake([UIScreen mainScreen].bounds.size.height-150-10, 289, 150, 21)];
            [theButton.titleLabel setTextAlignment:UITextAlignmentRight];
            [theButton setOpaque:YES];
            [theButton.titleLabel setFont:[UIFont fontWithName:@"MyFont" size:32.0]];
            [theButton.titleLabel setTextColor:[UIColor redColor]];
            [theButton.titleLabel setText:@"text"];
            [self addSubview:theButton];
            [theButton addTarget:myTarget action:@selector(pause) forControlEvents:UIControlEventTouchUpInside];

但是按钮不会按预期绘制。drawRect:如果我在和po [self subviews]llvm中设置断点,我会得到这个输出。

$0 = 0x0c93e860 <__NSArrayM 0xc93e860>(
<UIButton: 0xc93d910; frame = (10 289; 150 21); alpha = 0.5; layer = <CALayer: 0xc93d4a0>>,
<UIButton: 0xc93e2d0; frame = (408 289; 150 21); layer = <CALayer: 0xc93e390>>
)

那么为什么我的按钮不显示呢?

编辑:我的超级视图的递归描述:

<UIView: 0xa46b220; frame = (0 0; 320 568); transform = [0, -1, 1, 0, 0, 0]; autoresize = RM+BM; layer = <CALayer: 0xa46b280>>
   | <MyView: 0xa168710; frame = (0 0; 568 320); autoresize = W+H; layer = <CALayer: 0xa1687d0>>
   |    | <UIButton: 0xa16c160; frame = (10 289; 150 21); alpha = 0.5; layer = <CALayer: 0xa16bcc0>>
   |    |    | <UIButtonLabel: 0xa16c3e0; frame = (0 0; 0 0); clipsToBounds = YES; hidden = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xa16c480>>
   |    | <UIButton: 0xa16cb00; frame = (408 289; 150 21); layer = <CALayer: 0xa16cbc0>>
   |    |    | <UIButtonLabel: 0xa16c7e0; frame = (0 0; 0 0); clipsToBounds = YES; hidden = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xa16c880>>
4

2 回答 2

2

好吧,我的猜测是您的按钮正在显示,但是您将其设置为自定义按钮,没有正常状态的文本(并且因为您没有使用任何背景图像或颜色,它似乎不可见)。

UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"Text" forState:UIControlStateNormal];
于 2013-02-12T07:01:51.077 回答
0

不要调用setText按钮的标签,而是尝试发送setTitle:forState到按钮(不是它的标题标签):

[theButton setTitle:@"text" forState:UIControlStateNormal]
于 2013-02-12T07:01:42.530 回答