32

我想向导航栏添加分段控件,但也要保留标题和按钮,如 iOS 7 Appstore 购买部分(示例

我尝试将分段控件添加为标题视图,然后使用提示作为标题,但按钮与分段控件处于同一级别。

4

8 回答 8

15

我找到了两个解决方案:

1)根据neural5torm的建议,可以将分段控件添加到与导航栏背景颜色相同的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

- (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]];


标题和按钮项目将位于底部。调整它们的垂直位置(在自定义导航栏的初始化中或通过外观代理)

// Title view
[self setTitleVerticalPositionAdjustment:-dy forBarMetrics:UIBarMetricsDefault];

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

看 UIBarButtonItem 类参考,也有setTitlePositionAdjustment返回按钮等方法


创建分段控件时,将其添加到导航栏

[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;
    }
}
于 2013-10-18T19:28:48.810 回答
14

我尝试使用另一种方法解决您的问题,因为仅使用导航栏似乎没有解决问题(可能是因为 AppStore 应用程序使用的是私有 api,但我没有足够的知识来确定...)我只是使用放置在导航栏下方的工具栏,在该导航栏上添加了一个分段控件,所有这些都在常规 UIViewController 中。

这是 Storyboard 中的样子: 故事板

这是模拟器中的结果:

模拟器

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

于 2013-09-15T16:30:45.107 回答
9

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

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

// 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
于 2014-10-12T12:36:04.410 回答
7

您可以为此使用导航项 Prompt 属性。只需像这样在情节提要中设置属性,导航栏就会自动淡入淡出。唯一的缺点就是文字有点小。

在此处输入图像描述

于 2015-08-10T12:50:16.343 回答
2

我尝试在 Xamarin.iOS 中做到这一点,从 iOS 6 开始,您可以继承 UINavigationBar 并添加控件,您想要的按钮。

于 2013-09-26T13:50:24.170 回答
2

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

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

祝你好运!

于 2013-11-12T11:20:19.650 回答
1

我的解决方案是:
将工具栏和分段控件添加到您的xib文件中。根据需要自定义它并将其连接到视图控制器的插座:

在此处输入图像描述

然后,将其放入viewDidLoad方法中:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // add after your setup code
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:self.segmentedControl];

    self.navigationItem.rightBarButtonItem = item;
}

在此处输入图像描述

于 2014-02-03T12:33:15.980 回答
0

我还没有完全实现它,但以下是我打算做的。(ios7) 这将用于在同一个导航栏上并排放置标题和按钮。

在情节提要中,将空白视图添加到导航栏。然后将标签和分段控件添加到该视图。这允许您将任何您想要的控件添加到导航栏。到目前为止,用户界面可以正常工作,只是还没有连接起来。只是想分享我到目前为止的发现。

于 2014-05-14T16:49:07.583 回答