답변:
에서 이 답변
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
또한 Josh가 의견에서 제안한대로 막대를 기본값으로 다시 설정하십시오.
[self.navigationController.navigationBar setBackgroundImage:nil
forBarMetrics:UIBarMetricsDefault];
Swift3 및 Swift4의 경우
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
스위프트 2.2
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.translucent = true
오브젝티브 -C
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
Objective-C 카테고리로서의 자체 솔루션 :
UINavigationController + TransparentNavigationController.h
@interface UINavigationController (TransparentNavigationController)
- (void)presentTransparentNavigationBar;
- (void)hideTransparentNavigationBar;
@end
UINavigationController + TransparentNavigationController.m
#import "UINavigationController+TransparentNavigationController.h"
@implementation UINavigationController (TransparentNavigationController)
- (void)presentTransparentNavigationBar
{
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationBar setTranslucent:YES];
[self.navigationBar setShadowImage:[UIImage new]];
[self setNavigationBarHidden:NO animated:YES];
}
- (void)hideTransparentNavigationBar
{
[self setNavigationBarHidden:YES animated:NO];
[self.navigationBar setBackgroundImage:[[UINavigationBar appearance] backgroundImageForBarMetrics:UIBarMetricsDefault] forBarMetrics:UIBarMetricsDefault];
[self.navigationBar setTranslucent:[[UINavigationBar appearance] isTranslucent]];
[self.navigationBar setShadowImage:[[UINavigationBar appearance] shadowImage]];
}
@end
이제 카테고리를 UIViewController
가져와 내비게이션 컨트롤러에서 메소드를 호출 할 수 있습니다 ( 예 :
#import "UINavigationController+TransparentNavigationController.h"
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController presentTransparentNavigationBar];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController hideTransparentNavigationBar];
}
그리고 Swift 의 비슷한 솔루션 :
import Foundation
import UIKit
extension UINavigationController {
public func presentTransparentNavigationBar() {
navigationBar.setBackgroundImage(UIImage(), forBarMetrics:UIBarMetrics.Default)
navigationBar.translucent = true
navigationBar.shadowImage = UIImage()
setNavigationBarHidden(false, animated:true)
}
public func hideTransparentNavigationBar() {
setNavigationBarHidden(true, animated:false)
navigationBar.setBackgroundImage(UINavigationBar.appearance().backgroundImageForBarMetrics(UIBarMetrics.Default), forBarMetrics:UIBarMetrics.Default)
navigationBar.translucent = UINavigationBar.appearance().translucent
navigationBar.shadowImage = UINavigationBar.appearance().shadowImage
}
}
hideTransparentNavigationBar()
다시 재설정해야합니다.
viewDidHide
부모 뷰 컨트롤러 에서 호출 해보십시오 .
앨런 한 줄을 잊었다
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
그래서 나는 가지고있다:
[self.navigationController.navigationBar setTranslucent:YES];
self.navigationController.view.backgroundColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
@ Zorayr의 위대한 답변은 Swift 3로 개정되었습니다.
import Foundation
import UIKit
extension UINavigationController {
public func presentTransparentNavigationBar() {
navigationBar.setBackgroundImage(UIImage(), for:.default)
navigationBar.isTranslucent = true
navigationBar.shadowImage = UIImage()
setNavigationBarHidden(false, animated:true)
}
public func hideTransparentNavigationBar() {
setNavigationBarHidden(true, animated:false)
navigationBar.setBackgroundImage(UINavigationBar.appearance().backgroundImage(for: UIBarMetrics.default), for:.default)
navigationBar.isTranslucent = UINavigationBar.appearance().isTranslucent
navigationBar.shadowImage = UINavigationBar.appearance().shadowImage
}
}
스위프트 4.2 및 iOS 12
실제로 필요한 것은 아래 코드입니다. 당신이 그것을 넣을 때 완벽하게 작동합니다 viewDidLoad()
.
// removes line at bottom of navigation bar
navigationController?.navigationBar.shadowImage = UIImage()
// makes navigation bar completely transparent
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.isTranslucent = true
사용 UINavigationBar + 추가 포드, 단순히 전화 :
UINavigationBar *navigationBar = self.navigationController.navigationBar;
[navigationBar makeTransparent];
[(UIView *) [self.navigationController.navigationBar.subviews objectAtIndex : 0] setAlpha : 0.0f];
그 한 줄은 나에게 완벽하게 작동하는 것처럼 보였습니다.
self.edgesForExtendedLayout = UIRectEdgeNone;