【问题标题】:Subview added to image view is out of place添加到图像视图的子视图不合适
【发布时间】:2015-08-12 22:15:45
【问题描述】:

我正在尝试将子视图(模糊视图)添加到图像视图,但它将其放置在右下角。

这是我用来制作模糊视图的代码 (viewDidLoad):

UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
self.blurView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
self.blurView.frame = self.view.frame;

这是我用来将子视图添加到图像视图 (didSelectRowAtIndexPath) 的代码:

FriendsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FriendsTableViewCell" forIndexPath:indexPath];
[cell.chosenImage addSubview:self.blurView];

这是它的样子: http://i.stack.imgur.com/Hddej.png

它应该是这样的: http://i.stack.imgur.com/X4Isf.jpg

【问题讨论】:

  • 很难知道哪里出了问题,因为您的代码中有一些奇怪之处。例如,不要dequeue tableView:didSelectRowAtIndexPath: 中的单元格。请改用FriendsTableViewCell *cell = (FriendsTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];。此外,您在单元格存在之前在viewDidLoad 中设置模糊视图的框架,然后尝试向单元格添加视图。您需要根据单元格的contentView 设置边界。但更好的是在单元格的contentViewblurView 之间使用约束。我怀疑你还有其他问题。
  • 这就是问题所在。我改变了那条线,它起作用了。

标签: ios objective-c uiimageview subview


【解决方案1】:

您使用了self.view.frame 而不是self.view.bounds,因此模糊视图采用了 UITableViewCell 在其父级(UITableView)内的坐标。假设您只希望它们大小相同,请使用 UITableViewCell 的边界

有关坐标系的更详细说明,请参阅此处的“查看几何和坐标系”部分:https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/WindowsandViews/WindowsandViews.html

【讨论】:

  • 我改变了它,但它仍然产生相同的效果。当我将其更改为self.blurView.bounds = self.view.bounds 时,它会完全消失。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多