2013-02-07 13 views
6

Tôi muốn tạo thanh điều hướng tùy chỉnh giống như trong Ứng dụng BestBuy hoặc như được hiển thị trong ảnh chụp màn hình bên dưới.Làm cách nào để tạo Thanh Điều hướng Tùy chỉnh như Ứng dụng BestBuy?

enter image description here

Tôi muốn loại Navigation là luôn luôn trên đầu trang của mỗi và mọi viewController.

Nếu bất kỳ ai cũng có thể cho tôi biết quy trình hoặc bất kỳ trợ giúp nào sẽ được đánh giá cao. Cảm ơn.

+0

Tôi không nghĩ rằng họ đang sử dụng thanh điều hướng tùy chỉnh ... đó là hình ảnh đơn giản :) – NSCry

+0

họ có thể không .. Tôi đồng ý. câu trả lời của tôi là quá mức cần thiết cho điều này .. nó là linh hoạt nhất nhưng mã của aziz trông phù hợp hơn cho điều này tôi nghĩ. .. mặc dù tôi không biết về các dấu phân cách nền –

Trả lời

11

viết một lớp con của UINavigationBar mà bạn vẽ tùy chỉnh và thêm các bản xem phụ nếu cần.

sau đó cho navigationController của bạn để sử dụng lớp đó bởi initing nó bằng cách sử initWithNavigationBarClass:toolBarClass:

ví dụ

@interface MyBar : UINavigationBar 
@end 

@implementation MyBar 
.... //like any UIView 
@end 

UINavigationController *navi = [[UINavigationController alloc] initWithNavigationBarClass:[MyBar class] toolbarClass:nil]; 

thay vì initWithRootViewController


mẫu

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
// Override point for customization after application launch. 
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
    self.mainViewController = [[FDMainViewController alloc] initWithNibName:@"FDMainViewController_iPhone" bundle:nil]; 
} else { 
    self.mainViewController = [[FDMainViewController alloc] initWithNibName:@"FDMainViewController_iPad" bundle:nil]; 
} 

UINavigationController *navi = [[UINavigationController alloc] initWithNavigationBarClass:[UINavigationBar class] toolbarClass:nil]; 
navi.viewControllers = @[self.mainViewController]; 
self.window.rootViewController = navi; 
[self.window makeKeyAndVisible]; 
return YES; 
} 
+0

Làm cách nào để thông báo cho navigationController sử dụng lớp đó? –

+0

xem câu trả lời đã chỉnh sửa –

+0

Sau đó, cách tôi sẽ đặt bộ điều khiển gốc? –

4

Navigationbar thể mất ba quan điểm, hai nút ở mỗi bên trái và bên phải, bạn cũng có thể thêm một cái nhìn cho tiêu đề ,,

//this will set a background image to your navigation bar. 
[[self.navigationController navigationBar] setBackgroundImage:[UIImage imageNamed:@"top-bar.png"] forBarMetrics:UIBarMetricsDefault]; 

UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[leftButton setBackgroundImage:[UIImage imageNamed:@"backBtn.png"] forState:UIControlStateNormal]; 
leftButton.frame = CGRectMake(0, 0, 66, 30); 
[leftButton addTarget:self action:@selector(navBack) forControlEvents:UIControlEventTouchUpInside]; 
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton]; 

UIImage *image=[UIImage imageNamed:@"add-btn.png"]; 
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
button.bounds = CGRectMake(0, 0, image.size.width, image.size.height); 
[button setBackgroundImage:image forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(doneAct) forControlEvents:UIControlEventTouchUpInside]; 
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 

và để thêm vào thanh tìm kiếm

self.navigationItem.titleView = mySearchbarView; 
+0

có, nếu bạn không cần bộ tách ví dụ ... mặc dù những thứ đó có thể ở hình nền .. điều này có tác dụng đối với hầu hết các trường hợp –

+0

chắc chắn, nhưng sau đó nút sẽ không được đánh dấu khi chạm, câu trả lời của bạn sẽ tốt hơn để thực hiện. – Aziz

+0

cảm ơn, không thấy điều đó .. –