【问题标题】:UIButton losing target after rotationUIButton在旋转后失去目标
【发布时间】:2014-02-11 10:35:13
【问题描述】:

我有(/有)一个UITextField 子类,它有一个自定义删除按钮作为 rightView 属性。 这工作正常,直到我旋转设备然后按钮不再反应。 我改变了我的方法并“欺骗”了一点。现在我在代码中的UITextField 之上添加UIButton。在我旋转设备之前,它也可以正常工作。如果按钮顶部没有其他元素,我检查了 backgroundColors。

我还尝试移除按钮并在设备旋转时再次添加它。它是可见的,但该操作不会触发。如果我以纵向或横向方式启动我的应用程序,它会一直工作,直到我旋转设备。

如何添加按钮:

if (!btn_delete)
{
    btn_delete = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 70, (tf_search.frame.size.height / 2) - 8, 17, 16)];
    [btn_delete setImage:[UIImage imageNamed:@"icon_cross_textfield"] forState:UIControlStateNormal];
    [btn_delete addTarget:self action:@selector(clearTextField:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn_delete];
    [self.view bringSubviewToFront:btn_delete];
}
else
{
    [btn_delete removeFromSuperview];
    btn_delete = nil;
}

方法-(void)clearTextField:(UIButton*)sender在旋转后不会被调用。

感谢任何建议。

【问题讨论】:

  • 你应该在旋转后检查self.view的框架。您的 btw_delete 位于框架矩形之外。
  • 感谢您的建议。我用日志检查了它,但事实并非如此。
  • 检查userInteraction 并尝试将固定值添加到按钮框架。如果子视图在主视图范围之外,则触摸不会接收。
  • 如果您在模拟器中测试应用程序,那么您可以从 Debug->Color Blended Layers 调试调整大小问题,通过启用此应用程序将与不同的颜色重叠,这样您就可以轻松了解您的视图位置(如果您的按钮将超出它的 superView 绑定)。
  • 我主要在 iPad 上进行测试,但我遵循了您的建议 @Yuvrajsinh 但我没有发现任何问题。旋转后 UIButton 仍然在正确的 x、y 值上。

标签: ios objective-c rotation uibutton target


【解决方案1】:

UIButton 被添加到 UIViewController 的 UIView 中,该 UIView 是另一个 UIViewController 的 UIView 的一部分。它们都将 clearColour 作为 backgroundColor,因此 UIButton 仍然可见,但不在 superView 的范围内。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-05
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    相关资源
    最近更新 更多