UINavigationItem添加UISegmentControl

安明亮
2023-12-01
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:[UIImage imageNamed:@"grid.png"],[UIImage imageNamed:@"list.png"],nil]];
    [segmentedControl addTarget:self action:@selector(segmentedAction) forControlEvents:UIControlEventValueChanged];
    segmentedControl.frame = CGRectMake(0, 0, 90, 40);
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    segmentedControl.momentary = YES;
    [segmentedControl setTintColor:[UIColor clearColor]];

    UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];    
    self.navigationItem.rightBarButtonItem = segmentBarItem;


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

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


转帖: http://stackoverflow.com/questions/6850204/how-to-add-uisegmentcontrol-in-uinavigationitem

 类似资料: