UISegmentedControl嵌入在UINavigationBar / Item中 [英] UISegmentedControl embedded in a UINavigationBar/Item
问题描述
我想在 UINavigationController
的顶栏中的某处嵌入一个 UISegmentedControl
。
I would like to embed a UISegmentedControl
somewhere in my UINavigationController
s topbar.
将它嵌入 UIBarButtonItem
并将其设置为左或右barButtonItem是没有问题的。
It is no problem embedding it in a UIBarButtonItem
and setting it as the left or right barButtonItem.
在处理iPhone的屏幕空间时我能理解这种方法。但是,我在iPad上的Popover中执行此操作,并且顶部栏中有很多可用的垂直空间。如果我将segmentedControl添加为左侧或右侧barButtonItem,它会缩小,以便我看不到我的段按钮上的文本,它将成为完成按钮等的宽度。如果我尝试将其添加到navigationItem TitleView它将一直向右显示,并且仍然比我的3段控件缩小,文本可以显示。
I can understand this approach when dealing with the screen real-estate of an iPhone. I am, however, doing this in a Popover on an iPad and there is "lots" of vertical space available in the topbar. If I add the segmentedControl as a left or right barButtonItem it gets scaled down so that I can not see the text on my segment button, it gets to be the width of a "Done" button etc. If I try to add it to the navigationItem TitleView it will show up all the way to the right and still scaled down more than my 3 segment control with text can display.
我将如何添加 UISegmentedControl
到包裹我的popovers内容的 UINavigationController
的中心。
How would I go about adding a UISegmentedControl
to the center of the UINavigationController
that wraps my popovers content.
希望有人可以帮助我:)提前谢谢。
Hope someone can help me out:) thanks in advance.
推荐答案
为什么你需要把控件放在popover标题栏? iPad有更多的屏幕空间可以考虑将其放入下面的视图中。
Why would you need to put the control in the popover title bar? iPad has much more screen real estate to consider putting it into the view below.
- 编辑 -
我自己尝试过,它有效。以下是设置弹出控制器的代码:
I tried it myself and it works. Here is the code setting up the popover controller:
- (IBAction) showPopover: (id) sender
{
TestController *testController = [[TestController alloc] initWithStyle: UITableViewStylePlain];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: testController];
UIPopoverController *controller = [[UIPopoverController alloc] initWithContentViewController: navController];
[controller presentPopoverFromBarButtonItem: sender permittedArrowDirections: UIPopoverArrowDirectionAny animated: YES];
controller.delegate = self;
[testController release];
[navController release];
}
以下是TestController的实现:
Here is the implementation of TestController:
- (id) initWithStyle: (UITableViewStyle) style
{
if ( (self = [super initWithStyle: style]) ) {
UISegmentedControl *ctrl = [[UISegmentedControl alloc] initWithFrame: CGRectZero];
ctrl.segmentedControlStyle = UISegmentedControlStyleBar;
[ctrl insertSegmentWithTitle: @"One" atIndex: 0 animated: NO];
[ctrl insertSegmentWithTitle: @"Two" atIndex: 0 animated: NO];
[ctrl insertSegmentWithTitle: @"Three" atIndex: 0 animated: NO];
[ctrl sizeToFit];
// Any of the following produces the expected result:
self.navigationItem.titleView = ctrl;
//self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView: ctrl] autorelease];
[ctrl release];
}
return self;
}
结果如下:
除了发送 sizeToFit $ c $之外,我的代码中没有技巧c>到分段控制。这对你有用吗?
There are no tricks in my code besides sending sizeToFit
to the segmented control. Does this work for you?
这篇关于UISegmentedControl嵌入在UINavigationBar / Item中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!