【问题标题】:Position Toolbar on top of the screen with correct shadow in the bottom将工具栏定位在屏幕顶部,底部有正确的阴影
【发布时间】:2014-03-31 17:30:26
【问题描述】:

我有基于导航控制器的应用程序,一个视图控制器以横向模式呈现模态图。然后我添加带有完成按钮的工具栏以关闭图形 vc 并返回导航和纵向模式。

我不知道如何将工具栏放置在图形视图控制器的顶部,并在工具栏的底部显示正确的阴影。到目前为止,我有这段代码可以将工具栏添加到底部位置,该位置在工具栏顶部有默认阴影。是否允许在屏幕顶部有工具栏?由于强制方向旋转,我不能将导航控制器与图形 vc 一起使用。平台仅限 iOS7 和 iPhone。谢谢。

UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.size.width - 44.0, self.view.bounds.size.height, 44.0)];

UIBarButtonItem *flexibleSpaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

UIBarButtonItem *doneButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)];

toolbar.items = [NSArray arrayWithObjects:flexibleSpaceButtonItem, doneButtonItem, nil];

[self.view addSubview:toolbar];

【问题讨论】:

    标签: ios objective-c xcode toolbar


    【解决方案1】:

    我觉得你的框架看起来有点奇怪。您正在根据视图宽度和视图高度计算 y 位置。

    也许您必须使用UIBarPositioning 协议指定工具栏位于顶部。

    UIImage *shadow = [toolbar shadowImageForToolbarPosition: UIBarPositionAny];
    [toolbar setShadowImage:shadow forToolbarPosition:UIBarPositionTopAttached];
    

    下一个编辑:

    这是文档对 iOS 7 UIToolbar 的描述:

    UIBarPositionTop
    指定栏位于其包含视图的顶部。
    系统以此为提示,相应地绘制定向装饰。例如,任何阴影都将绘制在条形下方。 UIToolbar 的实例不会出现在 iPhone 上的这个位置,但它们可以出现在 iPad 上。
    适用于 iOS 7.0 及更高版本。
    UIBarCommon.h 中声明。

    也许工具栏不应该在顶部使用。但是,您可以使用 addSubview: 简单地添加阴影

    【讨论】:

    • 图形视图控制器以横向显示,因此我认为希望交换宽度和高度?
    • 查看我修改后的答案。
    • 我试过这个并且 xcode 抱怨'Assignment to readonly property' :(
    • 好的,我必须弄清楚如何正确设置它。问题可能是获取默认的阴影图像。您可以随时使用自己的。
    • 我显然在努力设置自己的阴影图像,看起来仍在使用默认值。我会继续尝试,但是当我移动栏时,阴影总是在顶部,永远不会出现在栏的底部。
    【解决方案2】:

    尝试实现方法

    - (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar 
    

    来自UIToolbarDelegate 协议。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-27
      • 2016-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多