How to add UISegmentControl in UINavigationItem ..?

前端 未结 1 632
被撕碎了的回忆
被撕碎了的回忆 2021-01-25 01:25

How do I add UISegmentedControl in UINavigationItem? I want to create a UINavigationBar with segment control which add in title of navigat

相关标签:
1条回答
  • 2021-01-25 01:52

    You're almost there, you just need to add the segmented control to a UINavigationItem and add that to your UINavigationBar:

    // This code is used for a custom navigation bar
    
    UINavigationItem* newItem = [[UINavigationItem alloc] initWithTitle:@""];
    [newItem setTitleView:segmentedControl];
    
    // Assuming you already have a navigation bar called "navigationBar"
    [navigationBar setItems:[NSArray arrayWithObject:newItem] animated:NO];
    
    // No memory leaks please...
    [newItem release];
    

    or if you want to use an existing controller

    // This is used for an existing navigation controller
    [navigationController.navigationBar.topItem setTitleView:segmentedControl];
    // or if you want to access through the root view controller of the nav controller
    [rootController.navigationItem setTitleView:segmentedControl];
    

    That should set the center view of your navigation bar to the segmented control.

    Hope I could help!

    EDIT: If you need more help, the Apple documentation of these classes is quite thorough:

    UINavigationItem

    UINavigationBar

    0 讨论(0)
提交回复
热议问题