Text in UITextField moves up after editing (center while editing)

前端 未结 13 1092
执念已碎
执念已碎 2020-12-01 03:14

I have a strange problem. I have an UITextField in which the user should write the amount of something, so the field is called \"amountField\". Everything looks fine, when t

相关标签:
13条回答
  • 2020-12-01 04:20

    I had similar issues with a UITextfield embedded in a UITableViewCell. Where exactly is this code located in your project? What I believe is happening is that after you've finished editing a particular textfield, it sends itself -setNeedsDisplay and its drawRect: is subsequently called. This might explain the shift in alignment. In my particular scenario, I had to use a table view delegate method -willDisplayCell... to set the content alignment. I would have to know more about your design to possibly offer a suggestion.

    One potential solution would be to use the text field delegate methods to set the content alignment.

    -(void)textFieldDidEndEditing:(UITextField *)textField{
         if (amountField == textField){
              amountField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
            }
    }
    
    0 讨论(0)
提交回复
热议问题