Add segmented control to navigation bar and keep title with buttons

后端 未结 8 2593
借酒劲吻你
借酒劲吻你 2020-12-04 10:03

I want to add segmented control to the navigation bar but also keep the title and buttons, like in iOS 7 Appstore purchased section (example)

I have tried adding seg

8条回答
  •  旧巷少年郎
    2020-12-04 10:39

    You can find navigation bar with UISegmentedControl in Apple Sample Code: https://developer.apple.com/library/ios/samplecode/NavBar/Introduction/Intro.html

    Here is my interpretation of this code (create programmatically):

    // File MySegmController.h
    @interface MySegmController : UIViewController
    @end
    
    // File MySegmController.m
    #import "MySegmController.h"
    
    @interface MyNavBarView : UIView
    @end
    
    @interface MySegmController ()
    {
        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
    

提交回复
热议问题