【问题标题】:Is there a preferred way to snap views to the bottom edge of their superviews for iOS 7 apps?是否有首选方法将视图捕捉到 iOS 7 应用程序的超级视图的底部边缘?
【发布时间】:2014-04-13 13:04:22
【问题描述】:

所以我有一个集合视图,它有一个过滤器按钮,我可以点击它来显示一个过滤器选择器。我的意图是让过滤器选择器显示在它添加到的视图的底部。在这种情况下,我将其添加到控制器视图中,例如self.view.

目前我正在这样做:

CGFloat viewHeight = [[self view] frame].size.height;
CGFloat pickerWidth = [[self view] bounds].size.width;
CGFloat pickerHeight;

UIPickerView *filterPicker = [[UIPickerView alloc] init];
pickerHeight = [filterPicker frame].size.height;
[filterPicker setFrame:CGRectMake(0, viewHeight - pickerHeight, pickerWidth, pickerHeight)];
[[self view] addSubview:filterPicker];

它在 iPhone 4 和 5 上都能正常工作。这有多可靠? 我还有其他方法吗?

我试过了:

[filterPicker setAutoresizingMask: UIViewAutoresizingFlexibleTopMargin];

但它没有任何效果。

问候

【问题讨论】:

  • 顺便说一句,您的 autoresizingMask 是错误的:如果您想将视图定位在 BOTTOM,您需要将遮罩设置为 UIViewAutoresizingFlexibleTopMargin,因为您希望底部边距固定为零,而不是灵活。
  • 复制并粘贴了错误的代码,但将其设置为顶部不起作用。
  • UIViewAutoresizingFlexibleTopMargin 应该可以工作,但如果启用了 AutoLayout 则可能不行...

标签: ios objective-c cocoa-touch uiview uicollectionview


【解决方案1】:

我想当你的 ViewController 支持旋转时你会遇到问题,你需要在旋转后重新布局 Picker 的位置。

我更喜欢使用自动布局 - 对于 Xcode 5,在界面构建器中使用自动布局比以前使用 Xcode 4 更容易。如果你想在代码中使用自动布局,请查看FLKAutolayout,这会更容易.

例如,将视图放在其父视图的底部会如下所示:

    UIView *view = [[UIView alloc] init];
    [superview addSubview:view];
    [view alignBottomEdgeWithView:superview predicate:@"0"];
    [view alignLeading:@"0" trailing:@"0" toView:superview];
    [view constrainHeight:@"44"];

这个例子在其父视图的底部对齐视图视图,将其高度固定为 44px,宽度与父视图的宽度相匹配。

【讨论】:

  • FLKAutolayout 可以派上用场。我无法在具有此特定视图的界面生成器中执行此操作,因此代码是我唯一的选择。考虑到我正在开发的应用程序不支持横向模式,你会说我上面显示的代码很好吗?
  • 只要您不想调整父视图的大小,您的代码应该没问题 - 但也许其他人知道我不知道的潜在问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-23
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多