在我的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>>
)
那么为什么我的按钮不显示?
编辑:我的superview的recursiveDescription:
<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>>
答案 0 :(得分:2)
嗯,我的猜测是你的按钮出现了,但你把它变成了一个没有正常状态文本的自定义按钮(因为你没有使用任何背景图像或颜色,它似乎看不见)。
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"Text" forState:UIControlStateNormal];
答案 1 :(得分:0)
不要在按钮的标签上调用setText
,而是尝试将setTitle:forState
发送到按钮(而不是其标题标签):
[theButton setTitle:@"text" forState:UIControlStateNormal]