【问题标题】:Cannot set button frame correctly over imageview in objective c无法在目标 c 中的 imageview 上正确设置按钮框架
【发布时间】:2015-04-13 20:39:55
【问题描述】:

我在图像视图上设置了一个按钮,但无法正确设置框架。我不明白为什么。这是问题的图片:https://docs.google.com/document/d/1ZrZs_eLfZaOov1cf6Hi0NA37MHLQmyf_j39g172lXXc/edit?usp=sharing

我尝试使用点击手势;那没有用。我尝试通过引用图像视图的边界和框架来设置框架,但这也不起作用

【问题讨论】:

    标签: objective-c uibutton imageview frame


    【解决方案1】:

    也许你应该阅读更多关于 UIView 的 framebound 属性之间的区别。 在您的代码中,您应该使用:

    clearButton.frame = [self.imageView convertRect:self.imageView.bounds toView:self.view];
    [self.view addSubview:clearButton];
    

    clearButton.frame = self.imageView.bounds;
    [self.imageView addSubview:clearButton];
    self.imageView.userInteractionEnabled = YES;
    

    设置 clearButton 的框架并将其添加到父视图。

    【讨论】:

    • 感谢您稍微居高临下和半正确的回答。您的第二个解决方案不起作用,因为按钮在放置为图像视图的子视图时不会触发。您的第一个解决方案生成的结果类似于我刚刚更新的 google 文档顶部的图像。
    • @djax imageView 是 self 的子视图吗?如果不是,我已经更新了第一个解决方案。我也更新了第二个。
    • 我已经更新了使用 UIView 的 convertRect:toView: 方法的第一个解决方案
    • clearButton.frame = [self.imageView convertRect:self.imageView.bounds toView:self.view];似乎也不起作用。
    • 也在行 clearButton.frame = [self.imageView convertRect:self.imageView.bounds toView:self];我收到“自我”的警告,因为那不应该是 self.view 吗?
    猜你喜欢
    • 2022-08-18
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    相关资源
    最近更新 更多