将分段控件添加到导航栏并使用button保留标题

我想添加分段控制到导航栏,但也保持标题和button,就像在iOS 7 Appstore购买部分( 示例 )

我已经尝试添加分段控制作为标题视图,然后使用提示作为标题,但是然后button与分段控制在同一水平。

我已经尝试使用另一种方法解决您的问题,因为使用导航栏似乎并没有工作(也许这是因为AppStore应用程序使用私人API,但我不够知识足以肯定…)反正我只是简单地使用了一个工具栏,在导航条的下面添加了一个分段控件,它们都在一个普通的UIViewController中。

这是故事板中的样子: 故事板

这是模拟器的结果:

模拟器

只要注意将表格视图向下偏移以考虑工具栏使用的垂直空间。 希望这可以帮助!

我发现两个解决scheme:

1)正如神经5tormbuild议,你可以添加分段的控制到一个UIView具有相同的导航栏的背景颜色

你可以这样删除UINavigationBar的发际线:

for (UIView *view in self.navigationController.navigationBar.subviews) { for (UIView *view2 in view.subviews) { if ([view2 isKindOfClass:[UIImageView class]]) { [view2 removeFromSuperview]; } } } 

不是半透明的导航栏。

如果你想要一个半透明的导航栏:
2)子类UINavigationBar通过重写sizeThatFits创build一个更高的酒吧

 - (CGSize)sizeThatFits:(CGSize)size { size.width = self.frame.size.width; size.height = your height (probably 88.0f); return size; } 

要使用您的自定义导航栏:

 UINavigationController *navController = [[UINavigationController alloc] initWithNavigationBarClass:[YouNavigationBar class] toolbarClass:nil]; [navController setViewControllers:@[viewController]]; 

标题和button项目将在底部。 调整其垂直位置(在自定义导航栏的init中或通过外观代理)

 // Title view [self setTitleVerticalPositionAdjustment:-dy forBarMetrics:UIBarMetricsDefault]; // Button item as icon/image [[UIBarButtonItem appearanceWhenContainedIn:[YourCustomNavigationBar class], nil] setBackgroundVerticalPositionAdjustment:-dy forBarMetrics:UIBarMetricsDefault]; 

看看UIBarButtonItem类的引用,还有setTitlePositionAdjustment和其他方法的后退button

在创build分段控件时,将其添加到导航栏中

 [self.navigationController.navigationBar addSubview:segmentedControl]; 

分段控制将在顶部。 通过在自定义导航栏中重写didAddSubview来调整其垂直位置

 - (void)didAddSubview:(UIView *)subview { [super didAddSubview:subview]; if ([subview isKindOfClass:[UISegmentedControl class]]) { CGRect frame = subview.frame; frame.origin.y += your extra height (probably 44.0f); subview.frame = frame; } } 

您可以在Apple示例代码中find带有UISegmentedControl的导航栏: https : //developer.apple.com/library/ios/samplecode/NavBar/Introduction/Intro.html

这是我对这段代码的解释(以编程方式创build):

 // File MySegmController.h @interface MySegmController : UIViewController @end // File MySegmController.m #import "MySegmController.h" @interface MyNavBarView : UIView @end @interface MySegmController ()<UITableViewDataSource, UITableViewDelegate> { UISegmentedControl* _segm; UITableView* _table; } @end #define SEGM_WIDTH 250 @implementation MySegmController - (void)loadView { [super loadView]; self.view.backgroundColor = [UIColor whiteColor]; self.title = @"Title"; float w = self.view.bounds.size.width; NSArray* items = [[NSArray alloc] initWithObjects: @"One", @"Two", @"Three", nil]; _segm = [[UISegmentedControl alloc] initWithItems: items]; [items release]; [_segm sizeToFit]; _segm.frame = CGRectMake((w - SEGM_WIDTH) / 2, 0, SEGM_WIDTH, _segm.bounds.size.height); _segm.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; _segm.selectedSegmentIndex = 0; MyNavBarView* topView = [[MyNavBarView alloc] initWithFrame: CGRectMake(0, 0, w, _segm.bounds.size.height + 10)]; topView.backgroundColor = [UIColor whiteColor]; topView.autoresizingMask = UIViewAutoresizingFlexibleWidth; [topView addSubview: _segm]; [_segm release]; _table = [[UITableView alloc] initWithFrame: CGRectMake(0, topView.bounds.size.height, w, self.view.bounds.size.height - topView.bounds.size.height) style: UITableViewStylePlain]; _table.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; _table.dataSource = self; _table.delegate = self; [self.view addSubview: _table]; [_table release]; // add topView AFTER _table because topView have a shadow [self.view addSubview: topView]; [topView release]; } - (void)viewDidLoad { [super viewDidLoad]; self.navigationController.navigationBar.translucent = NO; // pixel_transp.png - 1x1 image with transparent background self.navigationController.navigationBar.shadowImage = [UIImage imageNamed: @"pixel_transp"]; // pixel.png - 1x1 image with white background [self.navigationController.navigationBar setBackgroundImage: [UIImage imageNamed: @"pixel"] forBarMetrics: UIBarMetricsDefault]; UIBarButtonItem* bt = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCancel target: self action: @selector(onCancel)]; self.navigationItem.rightBarButtonItem = bt; [bt release]; } - (void)onCancel { [self.presentingViewController dismissViewControllerAnimated: YES completion: NULL]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 2; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: @"MyId"]; if (!cell) cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: @"MyId"] autorelease]; cell.textLabel.text = @"text"; return cell; } @end @implementation MyNavBarView - (void)willMoveToWindow: (UIWindow *)newWindow { self.layer.shadowOffset = CGSizeMake(0, 1.0f / UIScreen.mainScreen.scale); self.layer.shadowRadius = 0; self.layer.shadowColor = [UIColor blackColor].CGColor; self.layer.shadowOpacity = 0.25f; } @end 

您可以使用导航项目Prompt属性。 就像这样设置故事板的属性,导航栏会自动淡入淡出。 唯一的缺点是文字有点小。

在这里输入图像说明

我试图从Xamarin.iOS,从iOS 6,你可以inheritanceUINavigationBar并添加控件,button,你想要的。

尝试创buildUINavigationBar子类并让它符合UIToolbarDelegate协议。 然后在-init方法创build您的段控制,将其添加到UIToolBar并将其委托设置为您的自定义UINavigationBar类。 然后写下这个魔法:

 - (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar { return UIBarPositionTopAttached; } 

祝你好运!

我没有完全实现它,但是我打算做以下事情。 (ios7)这是为了让标题和button位于同一个导航栏上。

在故事板中,向导航栏添加一个空白视图。 然后将标签和分段控件添加到该视图。 这使您可以添加任何你想要的导航栏的控制。 用户界面工作到目前为止,只是没有把它连接起来。 只是想分享我迄今为止发现的。

我的解决scheme是:
添加工具栏和分段控制到你的xib文件。 根据需要自定义它,并将其连接到视图控制器的sockets上:

在这里输入图像说明

然后,把它放在viewDidLoad方法中:

 - (void)viewDidLoad { [super viewDidLoad]; // add after your setup code UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:self.segmentedControl]; self.navigationItem.rightBarButtonItem = item; } 

在这里输入图像说明