EXC_BAD_ACCESS when trying to get iPhone screen dimensions

≡放荡痞女 提交于 2019-12-02 04:05:28

问题


I'm trying to make certain elements resize according to the screen dimensions when the interface orientation changes. I've read in various places (including a few answers on here) that the way to get the bounds of the screen is to use [[UIScreen] mainScreen] bounds]. So I've added this code to see how it behaves:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    NSLog(@"view dimensions: (%@, %@)", [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
}

But when running it, as soon as I rotate the screen I get an EXC_BAD_ACCESS message. Is there something about the scope of the willRotateToInterfaceOrientation function that doesn't allow me to call for the screen bounds? If so, how would I go about it?


回答1:


%@ format specifier expects format parameter to be objective-c object, while what you provide is plain float. To print float numbers use %f specifier:

NSLog(@"view dimensions: (%f, %f)", [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);


来源:https://stackoverflow.com/questions/7710903/exc-bad-access-when-trying-to-get-iphone-screen-dimensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!