xcode UIButton 子类 - 设置属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7644617/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:04:11  来源:igfitidea点击:

UIButton Subclass - Setting Properties?

objective-ciosxcodeios4uibutton

提问by topLayoutGuide

I've created a Subclass of UIButton to give it a white 2px No Radii border, and now I'm trying to "globally" set it's font, font color, and background color depending on the button state.

我创建了一个 UIButton 的子类来给它一个白色的 2px No Radii 边框,现在我试图根据按钮状态“全局”设置它的字体、字体颜色和背景颜色。

The font does not set. Nor do the colors or the background colors. What's going on? Here's my code. I hope you can help :)

字体未设置。颜色或背景颜色也没有。这是怎么回事?这是我的代码。我希望你能帮助:)

- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {

    [[self titleLabel] setFont:[UIFont fontWithName:@"ZegoeUI" size:18]];
    [self setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    if(self.state == UIControlStateHighlighted) {
        [self setBackgroundColor:[UIColor whiteColor]];
    }
    else {
        [self setBackgroundColor:[UIColor blackColor]];
    }
}
return self;
}





// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {

             [self.layer setMasksToBounds:YES];
[self.layer setBorderWidth:2.0];
[self.layer setCornerRadius:0.0];
[self.layer setBorderColor:[[UIColor colorWithWhite:1.0 alpha:1.0] CGColor]];
}

I don't think I'm doing anything wrong. I have this class, and have linked Buttons in IB, and set the class type...

我不认为我做错了什么。我有这个类,并且在 IB 中链接了 Buttons,并设置了类类型...

Thanks,

谢谢,

BenBen

奔奔

回答by jrturton

If you have created the custom subclass buttons in IB, initWithFrame:will not be called. You need to override initWithCoder:, or, preferably, create a separate setup method that is called both from initWithFrame:and initWithCoder:.

如果在IB中创建了自定义子类按钮,initWithFrame:将不会被调用。您需要覆盖initWithCoder:,或者最好创建一个单独的 setup 方法,该方法同时调用 frominitWithFrame:initWithCoder:

For the first case:

对于第一种情况:

- (id)initWithCoder:(NSCoder*)coder {
self = [super initWithCoder:coder];
if (self) {

    [[self titleLabel] setFont:[UIFont fontWithName:@"ZegoeUI" size:18]];
    [self setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    if(self.state == UIControlStateHighlighted) {
        [self setBackgroundColor:[UIColor whiteColor]];
    }
    else {
        [self setBackgroundColor:[UIColor blackColor]];
    }
}
return self;
}

回答by Rudolf Adamkovi?

Here's how I do it (without IB):

这是我的做法(没有 IB):

+ (instancetype)customButtonWithCustomArgument:(id)customValue {
    XYZCustomButtom *customButton = [super buttonWithType:UIButtonTypeSystem];
    customButton.customProperty = customValue;
    [customButton customFunctionality];
    return customButton;
}

Works also with other types, UIButtonTypeSystemis just an example.

也适用于其他类型,UIButtonTypeSystem只是一个例子。

回答by Kirby Todd

UIButton is a class cluster. You really shouldn't be subclassing as the classes it represents are private.

UIButton 是一个类簇。你真的不应该子类化,因为它代表的类是私有的。