简体   繁体   中英

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. 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.

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. You can manipulate the hierarchy in 3D, filter for certain controls and more. 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.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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