最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】iOS中,在屏幕转换/切换时,如何实现自动或手动调整TextField的宽度

iOS crifan 2432浏览 0评论

之前虽然已经实现了对于屏幕旋转的支持,但是在旋转时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:

def-textfiled-width-168_thumb

切换到竖屏Landscape时的327的效果了:

new-textfiled-width-327_thumb

虽然目前此法不是足够好,不能实现自动根据屏幕宽度去按一定比例调整,但是也基本够用了。

转载请注明:在路上 » 【已解决】iOS中,在屏幕转换/切换时,如何实现自动或手动调整TextField的宽度

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.188 seconds, using 22.12MB memory