UISegmentedControl in the Navigation Bar with the Back button

后端 未结 2 1446
悲&欢浪女
悲&欢浪女 2021-02-02 15:03

I\'m adding a UISegmentedControl to the Navigation bar programatically where the titleView should be. But as Apple docs have mentioned under titl

2条回答
  •  南旧
    南旧 (楼主)
    2021-02-02 16:05

    You can create a UIBarButtonItem with a custom view which could potentially be your UISegmentedControl.

    Something along the lines of the following may work.

    //create segmented control with items
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"One", @"Two", nil]];
    
    //create bar button item with segmented control as custom view
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
    
    //add segmented control bar button item to navigation bar
    [[[self navigationController] navigationItem] setRightBarButtonItem:barButtonItem];
    

    I haven't tested this but it should be along the right lines of what you need.

提交回复
热议问题