隐形导航栏

发布于 2024-10-21 02:46:32 字数 67 浏览 5 评论 0原文

是否可以有一个不可见的导航栏背景?我以前见过定制的,但希望得到一些关于如何做到这一点的指导。

提前致谢!

Is it possible to have an invisible navigation bar background? I've seen custom ones before, but would love some guidance on how to do this.

Thanks in advance!

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

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

发布评论

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

评论(2

萝莉病 2024-10-28 02:46:32

要在 UINavigationBar 或 UIToolbar 上获得透明背景,您必须将背景颜色设置为 [UIColor clearColor],将 opaque 设置为 NO(如果尚未设置) ,并重写drawRect以不绘制标准渐变背景。第三是棘手的部分。

如果您直接使用 UINavigationBar,则只需对其进行子类化即可轻松覆盖 drawRect。但我看到您用 UINavigationController 标记了它,因此您必须尝试用类别覆盖它。像这样的事情应该可以做到:

@implementation UINavigationBar (invisibleBackground)

- (void)drawRect:(CGRect)rect {}

@end

它的缺点是应用程序中的每个导航栏现在都没有背景。如果你希望能够有一些透明和一些正常,你必须更进一步并混合 drawRect 以便你可以在需要时调用原始的:

#import <objc/runtime.h>

@implementation UINavigationBar (invisibleBackgroundSwizzle)

// The implementation of this method will be used to replace the stock "drawRect:". The old
// implementation of "drawRect:" will be attached to this name so we can call it when needed.
- (void)replacementDrawRect:(CGRect)rect {
    if (![self.backgroundColor isEqual:[UIColor clearColor]]) {
        // Non-transparent background, call the original method (which now exists
        // under the name "replacementDrawRect:"). Yes, it's a bit confusing.
        [self replacementDrawRect:rect];
    } else {
        // Transparent background, do nothing
    }
}

// This special method is called by the runtime when this category is first loaded.
+ (void)load {
    // This code takes the "drawRect:" and "replacementDrawRect:" methods and switches
    // thier implementations, so the name "drawRect" will now refer to the method declared as
    // "replacementDrawRect:" above and the name "replacementDrawRect:" will now refer
    // to the original implementation of "drawRect:".
    Method originalMethod = class_getInstanceMethod(self, @selector(drawRect:));
    Method overrideMethod = class_getInstanceMethod(self, @selector(replacementDrawRect:));
    if (class_addMethod(self, @selector(drawRect:), method_getImplementation(overrideMethod), method_getTypeEncoding(overrideMethod))) {
        class_replaceMethod(self, @selector(replacementDrawRect:), method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, overrideMethod);
    }
}

@end

To get a transparent background on a UINavigationBar or UIToolbar, you have to set the background color to [UIColor clearColor], set opaque to NO (if it isn't already), and override drawRect to not draw the standard gradient background. The third is the tricky part.

If you're using UINavigationBar directly, you can easily enough just subclass it to override drawRect. But I see you tagged this with UINavigationController, so you'll have to try overriding it with a category. Something like this should do it:

@implementation UINavigationBar (invisibleBackground)

- (void)drawRect:(CGRect)rect {}

@end

That has the drawback that every navbar in your app will now have no background. If you want to be able to have some transparent and some normal, you have to go one step further and swizzle drawRect so you can call the original when needed:

#import <objc/runtime.h>

@implementation UINavigationBar (invisibleBackgroundSwizzle)

// The implementation of this method will be used to replace the stock "drawRect:". The old
// implementation of "drawRect:" will be attached to this name so we can call it when needed.
- (void)replacementDrawRect:(CGRect)rect {
    if (![self.backgroundColor isEqual:[UIColor clearColor]]) {
        // Non-transparent background, call the original method (which now exists
        // under the name "replacementDrawRect:"). Yes, it's a bit confusing.
        [self replacementDrawRect:rect];
    } else {
        // Transparent background, do nothing
    }
}

// This special method is called by the runtime when this category is first loaded.
+ (void)load {
    // This code takes the "drawRect:" and "replacementDrawRect:" methods and switches
    // thier implementations, so the name "drawRect" will now refer to the method declared as
    // "replacementDrawRect:" above and the name "replacementDrawRect:" will now refer
    // to the original implementation of "drawRect:".
    Method originalMethod = class_getInstanceMethod(self, @selector(drawRect:));
    Method overrideMethod = class_getInstanceMethod(self, @selector(replacementDrawRect:));
    if (class_addMethod(self, @selector(drawRect:), method_getImplementation(overrideMethod), method_getTypeEncoding(overrideMethod))) {
        class_replaceMethod(self, @selector(replacementDrawRect:), method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, overrideMethod);
    }
}

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文