简体   繁体   English

旋转到横向后,UIButton不起作用

[英]UIButton doesn't work after rotate to landscape

The following is a snippet of the code being used to set up the subview layout on device rotate (I am using the same code for initial setup of the views): 以下是用于在设备旋转时设置子视图布局的代码的片段(我使用相同的代码进行视图的初始设置):

(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    if(screenBounds.size.height >= 1024) {
        [myButton setFrame:CGRectMake((screenBounds.size.width/3), (screenBounds.size.height/100)*81, (screenBounds.size.width/3), (screenBounds.size.height/100)*16)];
    }
    else {
        [myButton setFrame:CGRectMake((screenBounds.size.width/3), (screenBounds.size.height/100)*81, (screenBounds.size.width/3), (screenBounds.size.height/100)*16)];
    }
}

What is happening is myButton is being redrawn properly, but trying to interact with the button does nothing. 发生的是myButton正确地重绘了,但是尝试与按钮进行交互却无济于事。 If I go back to portrait, the button is able to be pressed after redraw. 如果我回到纵向,则可以在重画后按下该按钮。 If I start in landscape, the button works; 如果我从横向开始,则该按钮有效; but if I later rotate away and then back to landscape, button no longer works. 但是如果以后我转开然后又回到横向,按钮将不再起作用。 This has me stumped. 这让我难过。

So, it's very difficult to tell what's going on in your view without being able to see your storyboard, view hierarchy, etc. However, Xcode has made troubleshooting these types of issues much easier with Live View Debugging. 因此,很难在不查看情节提要,视图层次结构等情况下分辨视图中发生的事情。但是,Xcode通过Live View Debugging使对这类问题的疑难解答变得更加容易。

The first thing you can try is turning on view frames. 您可以尝试的第一件事是打开视框。 This will allow you to see the frames and bounds for each control. 这将允许您查看每个控件的框架和界限。 Most likely what you will see is that your button's frame is in one place, but the bounds is someplace else. 您最有可能看到的是按钮的框在一个位置,而边界在其他位置。 You can turn on view frames via Debug -> View Debugging -> Show View Frames. 您可以通过调试->视图调试->显示视图框架来打开视图框架。

在此处输入图片说明

If you need a more comprehensive overview of your hierarchy -- for example, maybe another view is covering the button -- Xcode now has a really cool feature to capture the view hierarchy. 如果您需要更全面的层次结构概述(例如,也许另一个视图覆盖了该按钮),那么Xcode现在具有捕获视图层次结构的强大功能。 You can manipulate the hierarchy in 3D, filter for certain controls and more. 您可以在3D模式下操作层次结构,为某些控件过滤等等。 This will allow you to visually inspect the hierarchy at run-time and see what might be going wrong with your button. 这将允许您在运行时直观地检查层次结构,并查看按钮可能出了什么问题。 Debug -> View Debugging -> Capture View Hierarchy. 调试->视图调试->捕获视图层次结构。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM