gpt4 book ai didi

ios - 如何在 UINavigationItem 中添加 UISegmentControl ..?

转载 作者:行者123 更新时间:2023-11-29 13:47:14 25 4
gpt4 key购买 nike

如何在 UINavigationItem 中添加 UISegmentedControl?我想创建一个带有段控件的 UINavigationBar,它添加在导航栏的标题中。

UISegmentedControl 有两个索引。

这是我所拥有的:

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;

我把它放在右边了。所以,也要放在导航栏中间。

这不起作用,如果我做错了什么,请告诉我。

谢谢

最佳答案

您就快完成了,您只需将分段控件添加到 UINavigationItem 并将其添加到您的 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];

或者如果你想使用现有的 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];

这应该将导航栏的中心 View 设置为分段控件。

希望我能帮上忙!

编辑:如果您需要更多帮助,这些类的 Apple 文档非常详尽:

UINavigationItem

UINavigationBar

关于ios - 如何在 UINavigationItem 中添加 UISegmentControl ..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6850204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com