drawRect在突出显示时未调用自定义UIButton子类

时间:2020-12-10 21:10:35

When using drawRect for a custom UIButton subclass, it never seems to get called to draw the button when highlighted. Do I need to call setNeedsDisplay for my button in my touch events?

当使用drawRect作为自定义UIButton子类时,似乎永远不会在突出显示时调用它来绘制按钮。我是否需要在触摸事件中为我的按钮调用setNeedsDisplay?

3 个解决方案

#1


As far as i can tell there is no straight forward way to subclass UIButton.

据我所知,没有直接的方法来继承UIButton。

UIButton is not the actual class type that is returned by the initializers. UIButton is kind of a front for a series of private classes.

UIButton不是初始化程序返回的实际类类型。 UIButton是一系列私人课程的前沿。

Say you had:

说你有:

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
NSLog(@"myButton type: %@", [myButton description]);

You will find the type returned in the log to be "UIRoundedRectButton". The problem with that is you would need to have extended "UIRoundedRectButton". That is not possible as it is a private class which is only ever returned to UIButton.

您会发现日志中返回的类型为“UIRoundedRectButton”。问题是你需要扩展“UIRoundedRectButton”。这是不可能的,因为它是一个只返回给UIButton的私人类。

On top of that "UIRoundedRectButton" is not the only possible returned class all of which are private.

最重要的是“UIRoundedRectButton”不是唯一可能返回的类,所有这些都是私有的。

In other words UIButton was built in manner that is not suited to be extended.

换句话说,UIButton的构建方式不适合扩展。

#2


I found an easy solution.

我找到了一个简单的解决方

Just add the following method to your UIButton subclass:

只需将以下方法添加到您的UIButton子类:

-(void)setHighlighted:(BOOL)highlighted
{
    [super setHighlighted:highlighted];
    [self setNeedsDisplay];
}

That's it!

#3


I had the same problem and satisfying success with the following added to my UIButton subclass

我有同样的问题并且满意成功,并将以下内容添加到我的UIButton子类中

- (void)awakeFromNib {
    [self addTarget:self action:@selector(redraw) forControlEvents:UIControlEventAllEvents];
}

- (void)redraw {
    [self setNeedsDisplay];
    [self performSelector:@selector(setNeedsDisplay) withObject:self afterDelay:0.15];
}

#1


As far as i can tell there is no straight forward way to subclass UIButton.

据我所知,没有直接的方法来继承UIButton。

UIButton is not the actual class type that is returned by the initializers. UIButton is kind of a front for a series of private classes.

UIButton不是初始化程序返回的实际类类型。 UIButton是一系列私人课程的前沿。

Say you had:

说你有:

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
NSLog(@"myButton type: %@", [myButton description]);

You will find the type returned in the log to be "UIRoundedRectButton". The problem with that is you would need to have extended "UIRoundedRectButton". That is not possible as it is a private class which is only ever returned to UIButton.

您会发现日志中返回的类型为“UIRoundedRectButton”。问题是你需要扩展“UIRoundedRectButton”。这是不可能的,因为它是一个只返回给UIButton的私人类。

On top of that "UIRoundedRectButton" is not the only possible returned class all of which are private.

最重要的是“UIRoundedRectButton”不是唯一可能返回的类,所有这些都是私有的。

In other words UIButton was built in manner that is not suited to be extended.

换句话说,UIButton的构建方式不适合扩展。

#2


I found an easy solution.

我找到了一个简单的解决方

Just add the following method to your UIButton subclass:

只需将以下方法添加到您的UIButton子类:

-(void)setHighlighted:(BOOL)highlighted
{
    [super setHighlighted:highlighted];
    [self setNeedsDisplay];
}

That's it!

#3


I had the same problem and satisfying success with the following added to my UIButton subclass

我有同样的问题并且满意成功,并将以下内容添加到我的UIButton子类中

- (void)awakeFromNib {
    [self addTarget:self action:@selector(redraw) forControlEvents:UIControlEventAllEvents];
}

- (void)redraw {
    [self setNeedsDisplay];
    [self performSelector:@selector(setNeedsDisplay) withObject:self afterDelay:0.15];
}