Uczyń UINavigationBar przezroczystym

Jak zrobić UINavigationBar przezroczysty ? Chociaż chcę, aby jego elementy barowe pozostały widoczne.

Author: EI Captain v2.0, 2010-02-23

15 answers

Jeśli ktoś zastanawia się, jak to osiągnąć w iOS 7+, Oto rozwiązanie (kompatybilne z iOS 6)

W Objective-C

[self.navigationBar setBackgroundImage:[UIImage new]
                         forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;

W swift 3 (iOS 10)

self.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.isTranslucent = true

W swift 2

self.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.translucent = true

Dyskusja

Ustawienie translucent na YES na pasku nawigacyjnym działa, ze względu na zachowanie omówione w UINavigationBar dokumentacja. Tutaj opiszę odpowiedni fragment:

Jeśli ustawisz tę właściwość na YES na pasku nawigacyjnym z nieprzezroczysty niestandardowy obraz tła, pasek nawigacji zastosuje krycie systemowe mniejsze niż 1,0 na obrazie.

 594
Author: Gabriele Petronella,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-27 10:50:46

W iOS5 możesz to zrobić, aby pasek nawigacji był przezroczysty:

nav.navigationBar.translucent = YES; // Setting this slides the view up, underneath the nav bar (otherwise it'll appear black)
const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];

[nav.navigationBar setBackgroundImage:maskedImage forBarMetrics:UIBarMetricsDefault]; 
[img release];
 29
Author: Alex Stanciu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-21 23:00:50

Z IOS7:

self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.view.backgroundColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
 23
Author: Damien Romito,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-01 19:49:45

Dla każdego, kto chce to zrobić w Swift 2.x:

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.translucent = true
Lub Swift 3.x:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
 14
Author: fuzz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-13 14:24:09

To chyba działa:

@implementation UINavigationBar (custom)
- (void)drawRect:(CGRect)rect {}
@end

navigationController.navigationBar.backgroundColor = [UIColor clearColor];
 10
Author: quano,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-04 21:44:39

Po zrobieniu tego, co kazdy inny powiedzial powyzej, tzn.:

navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController!.navigationBar.isTranslucent = true

... mój pasek nawigacyjny był wciąż biały . Więc dodałem ten wiersz:

navigationController?.navigationBar.backgroundColor = .clear

... et voila! To chyba zadziałało.

 7
Author: Clay Ellis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-18 23:57:34

Wiem, że ten temat jest stary, ale jeśli ludzie chcą wiedzieć, jak to zrobić bez przeciążania metody drawRect.

To jest to, czego potrzebujesz:

self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.opaque = YES;
self.navigationController.navigationBar.tintColor = [UIColor clearColor];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
 5
Author: Sander,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-21 13:31:37

Poniższy kod rozszerza się na górną odpowiedź wybraną dla tego wątku, aby pozbyć się dolnej granicy i ustawić kolor tekstu:

  1. Dwa ostatnie zakodowane wiersze tego kodu ustawiają przezroczystość. Pożyczyłem ten kod z tego wątku i zadziałał idealnie!

  2. Właściwość "clipsToBounds" była kodem, który znalazłem, który pozbył się dolnej linii granicznej z lub bez zestawu przezroczystości (więc jeśli zdecydujesz się przejść z pełnym białym / czarnym / itp. tło zamiast, nie będzie jeszcze nie będzie żadnej linii granicznej).

  3. Linia "tintColor" (druga linia kodowana) ustawia przycisk Wstecz na jasnoszary

  4. / Colspan = " 2 "Style =" border_style: None;" / Nie wiem, dlaczego przezroczystość nie działa, ale jeśli nie, chcę mój BG biały, jak kiedyś go miałem
    let navigationBarAppearace = UINavigationBar.appearance()
    navigationBarAppearace.tintColor = UIColor.lightGray
    navigationBarAppearace.barTintColor = UIColor.white
    navigationBarAppearace.clipsToBounds = true
    navigationBarAppearace.isTranslucent = true
    navigationBarAppearace.setBackgroundImage(UIImage(), for: .default)
    navigationBarAppearace.shadowImage = UIImage()
    
 3
Author: Dave G,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-10 23:40:53

Wypróbuj następujący fragment kodu:

self.navigationController.navigationBar.translucent = YES;
 2
Author: Russian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-26 06:06:15

Dla Swift 3.0:

override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    navigationController?.navigationBar.shadowImage = UIImage()
    navigationController?.navigationBar.isTranslucent = true
}
 2
Author: Wilson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-22 11:39:09

C# / Xamarin Solution

NavigationController.NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
NavigationController.NavigationBar.ShadowImage = new UIImage();
NavigationController.NavigationBar.Translucent = true;
 2
Author: Mark Moeykens,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-22 19:11:47

Innym sposobem, który zadziałał dla mnie, jest Podklasowanie UINavigationBar i pozostawienie metody drawrect pustej !!

@IBDesignable class MONavigationBar: UINavigationBar {


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
    // Drawing code
}}
 1
Author: M.Othman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-05 07:27:19

Masz na myśli całkowicie przezroczysty, czy używając przezroczystego-czarnego stylu widocznego w aplikacji Zdjęcia? Ten ostatni można osiągnąć ustawiając jego właściwość barStyle na UIBarStyleBlackTranslucent. Ten pierwszy... Nie jestem pewien. Jeśli chcesz, aby elementy na nim były nadal widoczne, być może będziesz musiał trochę poszperać w hierarchii widoków paska i usunąć widok zawierający jego tło.

 0
Author: Noah Witherspoon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-02-23 03:19:52

To działa dla Swift 2.0.

navigationController!.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
navigationController!.navigationBar.shadowImage = UIImage()
navigationController!.navigationBar.translucent = true
 0
Author: Chris3643,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-26 02:29:43

Check RRViewControllerExtension , który jest dedykowany do zarządzania wyglądem paska UINavigation.

Z RRViewControllerExtension w Twoim projekcie, musisz tylko nadpisać

-(BOOL)prefersNavigationBarTransparent;

In you viewcontroller.

pasek nawigacji tranparent

 0
Author: Roen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-27 01:49:00