推送新视图时删除工具栏

发布于 2024-09-02 10:08:15 字数 657 浏览 5 评论 0原文

在 iPhone 地图应用程序中,地图视图底部有一个工具栏(其中包含搜索/方向分段控件和其他控件)。通过单击标注从地图视图移出时,工具栏会随地图视图一起滑出,而下一个视图(表控制器)则没有工具栏。

我尝试在第二个视图控制器中使用 [self.navigationController setToolbarHidden:YESanimated:YES] 做同样的事情,但这给出了一个奇怪的工具栏向下滑动动画,而地图视图正在滑动向左转。

viewDidLoad 中使用 [self.navigationController setToolbarHidden:YES] 也会产生不好的效果(它使工具栏在推送动画开始时消失,留下难看的空白)。

我假设这个问题的答案是使用 nib 文件,但我更喜欢以编程方式执行此操作(如果可能)。

当我推送新的视图控制器时,如何让工具栏“粘”到地图视图并随之滑出?谢谢。

美食豪斯施塔特 http://img.skitch.com/20100518-xfubyriig48d3ckaemjg2ay8q.jpg

In the iPhone maps app there's a toolbar at the bottom of the map view (it contains the Search/Directions segment control and others). When moving from the map view by clicking on a callout, the toolbar slides out with the map view, leaving the next view (a table controller) with no toolbar.

I've tried to do the same thing with [self.navigationController setToolbarHidden:YES animated:YES] in the second view controller, but this gives a strange toolbar sliding down animation, while the map view is sliding to the left.

Using [self.navigationController setToolbarHidden:YES] in viewDidLoad also causes a bad effect (it makes the toolbar disappear the moment the push animation starts, leaving an ugly white space).

I'm assuming the answer to this is to use a nib file, but I'd prefer to do it programatically (if possible).

How can I get the toolbar to "stick" to the map view and slide out with it when I push a new view controller? Thanks.

Gourmet Haus Staudt http://img.skitch.com/20100518-xfubyriig48d3ckaemjg2ay8q.jpg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愛上了 2024-09-09 10:08:15

事实证明,答案是直接创建工具栏并自己将其添加到视图中。这是带有 UINavigationControllerUIViewController 的代码。框架坐标可以根据屏幕上的内容而变化。

- (void)viewDidLoad
{
    // Add a toolbar to the view
    CGRect toolbarFrame = CGRectMake(0, 372, 320, 44);
    UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];

    UIBarButtonItem *compassButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"compass.png"]
                                                                      style:UIBarButtonItemStyleBordered
                                                                     target:self
                                                                     action:@selector(zoomToCurrentLocation)];

    compassButton.width = 30.0f; // make the button a square shape
    [myToolbar setItems:[NSArray arrayWithObject:compassButton] animated:NO];
    [compassButton release];

    [self.view addSubview:myToolbar];
    [super viewDidLoad];
}

It turns out the answer is to create the toolbar directly and add it to the view yourself. This is in the code for a UIViewController with a UINavigationController. The frame coordinates can change according to what is on screen.

- (void)viewDidLoad
{
    // Add a toolbar to the view
    CGRect toolbarFrame = CGRectMake(0, 372, 320, 44);
    UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];

    UIBarButtonItem *compassButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"compass.png"]
                                                                      style:UIBarButtonItemStyleBordered
                                                                     target:self
                                                                     action:@selector(zoomToCurrentLocation)];

    compassButton.width = 30.0f; // make the button a square shape
    [myToolbar setItems:[NSArray arrayWithObject:compassButton] animated:NO];
    [compassButton release];

    [self.view addSubview:myToolbar];
    [super viewDidLoad];
}
强者自强 2024-09-09 10:08:15

我曾经在这里呆过一天。确实没有得到编程答案,但视图正确运行的最佳方法是在界面生成器中创建界面。如果您在代码中为工具栏设置项目,例如:
[self.navigationController setToolbarItems:control1,control2,...,nil]动画:NO];

以我的一点经验,我可以说,您是在对整个应用程序说,当您推送新视图时,要显示一个工具栏,除非您隐藏它(或者您正在使用 tabBar),但是隐藏它会产生那些不需要的效果。

你可以试试这个:

[self.navigationController setToolbarHidden:YESanimated:YES];

在你的第一个控制器中 - (void)viewWillDisappear:(BOOL)animated 方法,
并在第一个控制器中的 - (void)viewWillAppear:(BOOL)animated 方法中将隐藏设置为 NO。

希望这有帮助。

PS:如果您得到编程答案,请告诉我! =P

I was around this for a day once. Really dont get the programatically answer, but the best way to views to behave correctly, is to do the interface in the interface builder. If you set items for a toolbar in your code like:
[self.navigationController setToolbarItems: control1, control2,..., nil] animated: NO];

with my little experience, I can say that you are saying to the entire application to have a toolbar present when you push new views unless you hide it (or you are using a tabBar), but hiding it you get those unwanted effects.

You can try this:

[self.navigationController setToolbarHidden:YES animated:YES];

in your first controller - (void)viewWillDisappear:(BOOL)animated method,
and setting hidden to NO in - (void)viewWillAppear:(BOOL)animated method in the first controller too.

Hope this helps.

PS: And if you get the programatically answer, let me know! =P

眼眸 2024-09-09 10:08:15

重写第二个视图控制器的 -viewWillAppear: 方法以隐藏工具栏。

Override the second view controller's -viewWillAppear: method to hide the toolbar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文