Custom nav bar styling-iOS [duplikat]
Możliwy duplikat:
Jak dodać obraz tła na pasku nawigacyjnym iphone ' a?
IOS-jak NY Times zrobił tę niestandardową stylizację górnego paska nawigacyjnego?
A jeśli o to chodzi, ten dolny?
Dzięki...7 answers
EDIT: to jest nieaktualne; dla iOS5 jest much better answer below , by @ Jenox.
Całkowicie niestandardowa stylizacja pasków nawigacyjnych jest zaskakująco trudna. Najlepszy tekst jaki znam to ten Sebastian Celis: http://sebastiancelis.com/2009/12/21/adding-background-image-uinavigationbar/
To nie nadpisuje drawRect i zawiera dobre wyjaśnienie, dlaczego to jest dobra rzecz. Pamiętaj również, że nie musisz postępować zgodnie z jego samouczkiem. Możesz Pobierz kompletny kod tutaj: https://github.com/scelis/ExampleNavBarBackground
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-05-23 12:25:06
@ ludwigschubert rozwiązanie jednak nie działa na iOS 5. Tak samo jak nadpisywanie -drawRect:
, ponieważ nie jest nawet wywołane.
W systemie iOS 5 pasek nawigacyjny składa się z UINavigationBarBackground
i UINavigationItemView
. Istnieją dwa inne sposoby, aby to zadziałało.
Wstaw swój własny widok obrazu w index 1 zamiast 0. Sprawia to, że jest on wyświetlany nad natywnym obrazem tła, pozostając pod przyciskami.
-
Korzystaj z iOS 5 UIAppearance protokół . Możesz ustawić obraz tła dla wszystkich
[[UINavigationBar appearance] setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault]
Lub tylko dla jednego paska nawigacyjnego:
[navigationController.navigationBar setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault]
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-11-07 22:36:44
Aby dodać do odpowiedzi udzielonej przez @ Jenox, jeśli chcesz obsługiwać oba iOS 4.xx i iOS 5.urządzenia xx (tzn. Twój DeploymentTarget to 4.xx), należy zachować ostrożność podczas zawijania wywołania do Proxy appearance, sprawdzając w czasie wykonywania, czy selektor 'appearance' jest obecny, czy nie.
Możesz to zrobić przez://Customize the look of the UINavBar for iOS5 devices
if ([[UINavigationBar class]respondsToSelector:@selector(appearance)]) {
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavigationBar.png"] forBarMetrics:UIBarMetricsDefault];
}
Należy również opuścić iOS 4.XX obejście, które możesz wdrożyć. Jeśli wdrożyłeś obejście "drawRect" dla iOS 4.urządzenia xx, jak wspomniano przez @ludwigschubert, powinieneś to zostawić w:
@implementation UINavigationBar (BackgroundImage)
//This overridden implementation will patch up the NavBar with a custom Image instead of the title
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"NavigationBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
Spowoduje to, że pasek nawigacyjny będzie wyglądał tak samo w urządzeniach z iOS 4 i IOS 5.
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-10-19 14:59:57
Skopiuj to do viewDidLoad. Sprawdzi dla iOS 5 i użyje preferowanej metody, w przeciwnym razie doda subview do paska nawigacyjnego dla wersji IOS
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
NSLog(@"%f",version);
UIImage *backgroundImage = [UIImage imageNamed:@"myBackgroundImage.png"];
if (version >= 5.0) {
[self.navigationController.navigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
}
else
{
[self.navigationController.navigationBar insertSubview:[[[UIImageView alloc] initWithImage:backgroundImage] autorelease] atIndex:1];
}
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-06-25 19:47:58
Możesz po prostu utworzyć kategorię i utworzyć niestandardową metodę, aby dodać dowolny widok, który chcesz - obrazy, przyciski, suwaki. Przykład Foe, oto kod, którego używam-dodaje custom backgroundimage, backButton i Label.
@interface UINavigationBar (NavigationBar)
-(void)setBarForCard;
@end
@implementation UINavigationBar (NavigationBar)
-(void)setBarForCard
{
UIImageView *aTabBarBackground = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"BarImage"]];
aTabBarBackground.frame = CGRectMake(0,0,self.frame.size.width,44);
[self addSubview:aTabBarBackground];
[self sendSubviewToBack:aTabBarBackground];
[aTabBarBackground release];
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
backBtn.frame =CGRectMake(10, 8, 60, 30);
[backBtn addTarget:self action:@selector(back:)forControlEvents:UIControlEventTouchUpInside];
[backBtn setImage:[UIImage imageNamed: @"Back"] forState:UIControlStateNormal];
[self addSubview:backBtn];
UILabel *calendar = [[UILabel alloc]init];
calendar.frame = CGRectMake(105, 13, 109, 21);
calendar.text = @"Calendar"
calendar.textColor = [UIColor whiteColor];
calendar.textAlignment = UITextAlignmentCenter;
calendar.shadowColor = [UIColor grayColor];
calendar.shadowOffset = CGSizeMake(0, -1);
calendar.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:20];
calendar.backgroundColor = [UIColor clearColor];
[self addSubview:calendar];
}
A następnie, w dowolnym kontrolerze widoku, możesz zmienić pasek nawigacyjny, wywołując [self.navigationController.navigationBar setBarForCard];
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-03-10 09:11:50
To jest lepszy sposób na iOS 5
if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
UIImage *image = [UIImage imageNamed:@"navBarImg.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
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-05-16 18:11:24
Możesz zmienić kolor tinty paska nawigacyjnego, aby zmienić jego kolor, a także możesz użyć obrazu w widoku paska nawigacyjnego. Dla dolnego paska myślę, że używają widoku z trzema niestandardowymi przyciskami.
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-04-07 04:08:23