简体   繁体   English

UITextField不能占据整个屏幕宽度

[英]UITextField doesn't take whole screen width

I'm using a UITextField and a UIButton as you can see in this screenshot: 如下面的屏幕截图所示,我正在使用UITextField和UIButton:

在此处输入图片说明

Sometimes the "test" button needs to be disabled so I disable it with this code: 有时需要禁用“测试”按钮,因此我使用以下代码将其禁用:

    [_hButton setEnabled:NO];
    [_hButton setHidden: true]; 

But the problem is that the UITextField doesn't take the whole width from the screen like you can see here: 但是问题在于,UITextField不会像您在此处看到的那样占用屏幕的整个宽度:

在此处输入图片说明

This are the settings from the UITextField in my storyboard: 这是我的故事板上的UITextField的设置:

在此处输入图片说明

Any idea how I can fix this? 知道我该如何解决吗?

When you doing this [_hButton setEnabled:NO]; [_hButton setHidden: true]; 当您执行此操作时[_hButton setEnabled:NO]; [_hButton setHidden: true]; [_hButton setEnabled:NO]; [_hButton setHidden: true]; , you have to modify textField width. ,则必须修改textField的宽度。

Your code should be.. 您的代码应该是..

CGRect oldFrame = yourTextField.frame;
CGRect frameNew = CGRectMake(oldFrame.origin.x,oldFrame.origin.y,oldFrame.size.width + _hbutton.frame.size.width,oldFrame.size.height);
yourTexField.frame = frameNew;

When you want back this, just do reverse calculation. 当您想要返回时,只需进行反向计算即可。

Although the button isHidden, it is still there. 尽管该按钮处于隐藏状态,但它仍然存在。 You should remove it from the view completely for the desired effect [_hButton removeFromSuperview]; 您应该将其从视图中完全删除以达到所需的效果[_hButton removeFromSuperview];

UITextView is not resizing since the button is still there, it's just not visible. 由于按钮仍然存在,因此UITextView不会调整大小,只是不可见。 After you hide the button, adjust UItextView frame manually. 隐藏按钮后,手动调整UItextView框架。

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

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