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

san*_*i93 27 uinavigationbar uisegmentedcontrol ios

我想在导航栏中添加分段控件,同时保留标题和按钮,就像在iOS 7 Appstore中购买的部分一样(示例)

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

use*_*773 15

我找到了两个解决方案

1)正如neur5torm所建议的那样,您可以将分段控件添加到具有相同导航栏背景颜色的UIView中

您可以通过以下方式删除UINavigationBar的发际线:

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

这对于不是半透明的导航栏是可以的.



如果你想要一个半透明的导航栏:
2)子类UINavigationBar通过覆盖创建一个更高的栏sizeThatFits

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


要使用自定义导航栏:

UINavigationController *navController = [[UINavigationController alloc] initWithNavigationBarClass:[YouNavigationBar class] toolbarClass:nil];
[navController setViewControllers:@[viewController]];
Run Code Online (Sandbox Code Playgroud)


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

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

// Button item as icon/image 
[[UIBarButtonItem appearanceWhenContainedIn:[YourCustomNavigationBar class], nil] setBackgroundVerticalPositionAdjustment:-dy forBarMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)

查看UIBarButtonItem类的引用,还有setTitlePositionAdjustment后退按钮的其他方法


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

[self.navigationController.navigationBar addSubview:segmentedControl];
Run Code Online (Sandbox Code Playgroud)


分段控件将位于顶部.通过覆盖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;
    }
}
Run Code Online (Sandbox Code Playgroud)


neu*_*orm 12

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

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

这是模拟器中的结果:

模拟器

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

  • 我之前已经考虑过这个问题,这个方法唯一的问题是导航栏和分段控件之间的水平接缝. (6认同)

小智 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
Run Code Online (Sandbox Code Playgroud)


Zee*_*zer 6

您可以使用导航项提示属性.只需在故事板中设置属性,导航栏就会自动淡入淡出.唯一的缺点是文字有点小.

在此输入图像描述