如何使用更高版本的 iOS 4.2 覆盖 UIToolbar 方法

发布于 2024-12-04 08:57:27 字数 873 浏览 0 评论 0原文

此代码适用于 iPad Simulator 4.2,但不适用于更高版本的 iOS4.3 或更高版本。我无法覆盖 UIToolbar 类方法。

@implementation UIToolbar (CustomImage)
- (void)drawRect:(CGRect)rect 
{
    UIImage *image = [[UIImage imageNamed:@"ToolBar.png"] retain];
    [image drawInRect:rect];
    [image release];    
}
 //return 'best' size to fit given size. does not actually resize view. Default is return existing view size
- (CGSize)sizeThatFits:(CGSize)size {
    CGSize result = [super sizeThatFits:size];
    result.height = 80;
    return result;
};  

替代解决方案是什么?请指导我。 在更高版本中..- (void)drawRect:(CGRect)rect 永远不会被调用。

使用 iPad Simulator 4.2 代码运行可以正常运行,但使用 iPad Simulator 4.3 时,drawRect 从未被调用过。

下面是工具栏的屏幕截图:

在此处输入图像描述

在此处输入图像描述

This code works fine with iPad Simulator 4.2, but not with later version of iOS4.3 or after that.I am not able to Override the UIToolbar class methods.

@implementation UIToolbar (CustomImage)
- (void)drawRect:(CGRect)rect 
{
    UIImage *image = [[UIImage imageNamed:@"ToolBar.png"] retain];
    [image drawInRect:rect];
    [image release];    
}
 //return 'best' size to fit given size. does not actually resize view. Default is return existing view size
- (CGSize)sizeThatFits:(CGSize)size {
    CGSize result = [super sizeThatFits:size];
    result.height = 80;
    return result;
};  

What would be the alternate solution for this ?Please guide me.
In later version ..- (void)drawRect:(CGRect)rect is never called .

Running with iPad Simulator 4.2 code works fine but with iPad Simulator 4.3 drawRect in never called.

Below is the Screenshot of Toolbar:

enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掐死时间 2024-12-11 08:57:27

像这样的事怎么办?

@implementation UIToolbar (UIToolbarCategory)
- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    if([self isMemberOfClass: [UIToolbar class]]){
        [super drawRect:rect];
        UIImage *image = [UIImage imageNamed:@"bar_gradient.png"];
        [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    }
}
@end

What about something like this?

@implementation UIToolbar (UIToolbarCategory)
- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    if([self isMemberOfClass: [UIToolbar class]]){
        [super drawRect:rect];
        UIImage *image = [UIImage imageNamed:@"bar_gradient.png"];
        [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    }
}
@end
烟凡古楼 2024-12-11 08:57:27

如果要将其实现为类别,则需要根据 iOS5 更改日志对 UIToolBar 进行子类化

在 iOS 5 beta 中,UINavigationBar,
UIToolbar 和 UITabBar 实现已更改,以便
drawRect:不会在这些类的实例上调用方法,除非
是在子类中实现的。

在其中任何一个的类别中重新实现了drawRect:的应用程序
类会发现drawRect:方法没有被调用。

UIKit 进行链接检查以防止该方法在应用程序中被调用
iOS 5 之前已链接,但 iOS 5 或不支持此设计
之后。应用程序可以:

  • 使用 iOS 5 及更高版本中的栏自定义 API,这是首选方式。
  • 子类 UINavigationBar(或其他栏类)并在子类中重写 drawRect: 。

you are implementing it as a category, you need to subclass UIToolBar based on the iOS5 change log

In the iOS 5 beta, the UINavigationBar,
UIToolbar, and UITabBar implementations have changed so that the
drawRect: method is not called on instances of these classes unless it
is implemented in a subclass.

Apps that have re-implemented drawRect: in a category on any of these
classes will find that the drawRect: method isn’t called.

UIKit does link-checking to keep the method from being called in apps
linked before iOS 5 but does not support this design on iOS 5 or
later. Apps can either:

  • Use the customization API for bars that in iOS 5 and later, which is the preferred way.
  • Subclass UINavigationBar (or the other bar classes) and override drawRect: in the subclass.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文