xcode 如何在 Interface Builder 中使用自定义 UIButton?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13786251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use a custom UIButton with Interface Builder?
提问by gabor.orosz
I have a subclass UIButton that loads a Nib:
我有一个加载笔尖的子类 UIButton:
@implementation BlaButtonVC
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSArray *subViews = [[NSBundle mainBundle] loadNibNamed:@"BlaButton" owner:self options:nil];
[self addSubview:subViews[0]];
}
return self;
}
@end
And I can add this button to a view, like this:
我可以将此按钮添加到视图中,如下所示:
// ViewController.m
BlaButtonVC *button = [[BlaButtonVC alloc] initWithFrame:CGRectMake(10, 10, 280, 44)];
button.titleXLabel.text = @"Nyuff";
button.descLabel.text = @"Kukk";
[self.view addSubview:button];
My problem is I don't know how to do it from Interface Bulder, how to use this UIButton subclass instead of the normal one.
我的问题是我不知道如何从 Interface Bulder 做到这一点,如何使用这个 UIButton 子类而不是普通的子类。
Already changed Button Type to Custom, and Custom Class to BlaButtonVC, but it's not enough. It'll call BlaButtonVC initWithFrame, but button will not appear nor in Interface Builder, nor in Simulator.
已经将 Button Type 更改为 Custom,将 Custom Class 更改为 BlaButtonVC,但这还不够。它会调用 BlaButtonVC initWithFrame,但按钮不会出现在 Interface Builder 中,也不会出现在 Simulator 中。
What did I missed?
我错过了什么?
Here you can find the full sample:
https://www.dropbox.com/s/ioucpb6jn6nr0hs/blabla1.zip
在这里您可以找到完整的示例:https:
//www.dropbox.com/s/ioucpb6jn6nr0hs/blabla1.zip
回答by Martol1ni
When initialized from the StoryBoard
, it calls the method initWithCoder:
instead of initWithFrame:
. Aside from that, everything is correct.
从 初始化时StoryBoard
,它调用方法initWithCoder:
而不是initWithFrame:
。除此之外,一切都是正确的。
回答by Tricertops
In Interface Builder you can not add subviews to UIButton
, so here are some ideas:
在 Interface Builder 中,您不能向 中添加子视图UIButton
,因此这里有一些想法:
- Do it in code.
- Place the button below the labels and image, so it would receive touches and work as expected.
- Use
UITableView
with cells (this looks like what you are trying to achieve). - Use custom
UIView
withUITapGestureRecognizer
.
- 用代码来做。
- 将按钮放在标签和图像下方,这样它就会接收触摸并按预期工作。
UITableView
与单元格一起使用(这看起来像您要实现的目标)。- 将自定义
UIView
与UITapGestureRecognizer
.