之前虽然已经实现了对于屏幕旋转的支持,但是在旋转时TextField类型的输入框的宽度不能调整,用户体验不好,所以下面就继续折腾,如何解决此问题:
针对此问题,就又找了参考资料:
iOS 4 iPhone Rotation, View Resizing and Layout Handling
得知是在函数willAnimateRotationToInterfaceOrientation中实现。
然后又参考:Change Width of TextField去搞懂,如何给TextField的宽度赋值。
经过一番调试,最终结果是:
在AddSightingViewController.m中添加如下代码:
-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ //_birdNameInput.frame = CGRectMake(_birdNameInput.frame.origin.x, _birdNameInput.frame.origin.y, _birdNameInput.frame.size.width + 40, _birdNameInput.frame.size.height); //_locationInput.frame = CGRectMake(_locationInput.frame.origin.x, _locationInput.frame.origin.y, _locationInput.frame.size.width + 60, _locationInput.frame.size.height); if((toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)){ _birdNameInput.frame = CGRectMake(_birdNameInput.frame.origin.x, _birdNameInput.frame.origin.y, 327, _birdNameInput.frame.size.height); _locationInput.frame = CGRectMake(_locationInput.frame.origin.x, _locationInput.frame.origin.y, 327, _locationInput.frame.size.height); } else if((toInterfaceOrientation == UIInterfaceOrientationPortrait) || (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)){ _birdNameInput.frame = CGRectMake(_birdNameInput.frame.origin.x, _birdNameInput.frame.origin.y, 168, _birdNameInput.frame.size.height); _locationInput.frame = CGRectMake(_locationInput.frame.origin.x, _locationInput.frame.origin.y, 168, _locationInput.frame.size.height); } }
然后最后就可以实现对应的,在屏幕从横屏Portrait时的TextField的宽度为168:
切换到竖屏Landscape时的327的效果了:
虽然目前此法不是足够好,不能实现自动根据屏幕宽度去按一定比例调整,但是也基本够用了。