溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

解決iOS UITextField 編輯時(shí)文本偏移問(wèn)題

發(fā)布時(shí)間:2020-10-19 04:01:30 來(lái)源:腳本之家 閱讀:211 作者:sw_gegewu 欄目:移動(dòng)開(kāi)發(fā)

 1.在cell中添加UITextField,在編輯的時(shí)候總是出現(xiàn)文本偏移問(wèn)題,編輯結(jié)束時(shí)回復(fù)正常,解決方式如下:

(1)代碼創(chuàng)建UITextField,并添加上去,設(shè)置textField的一些屬性:

- (UITextField *)textField{
 if (_textField == nil) {
  _textField = [UITextField new];
  _textField.borderStyle = UITextBorderStyleNone;
  _textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  _textField.textAlignment = NSTextAlignmentLeft;
  _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
  _textField.tintColor = [UIColor redColor];
  _textField.font = GXFont(16);
  _textField.returnKeyType = UIReturnKeyDone;
  _textField.delegate = self;
 }
 return _textField;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
 if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  [self.contentView addSubview:self.textField];
 }
 return self;
}
- (void)layoutSubviews{
 [super layoutSubviews];
 self.textField.frame = (CGRect){{kDefaultSpace, 0},{GXScreenWidth-2*kDefaultSpace, CGRectGetHeight(self.frame)}};
}

(2)xib創(chuàng)建,并拉線成屬性:

解決iOS UITextField 編輯時(shí)文本偏移問(wèn)題

代碼中設(shè)置屬性:

 self.textFidle.textAlignment = NSTextAlignmentLeft;
 self.textFidle.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
 self.textFidle.borderStyle = UITextBorderStyleNone;
 self.textFidle.backgroundColor = [UIColor lightGrayColor];

設(shè)置成功,如果還不成功的話,就要吧各個(gè)屬性都測(cè)試一下,

注意:self.textFidle.clipsToBounds = YES;這句話好像對(duì)偏移有影響,大家注意一下,我測(cè)試的是如果在上面的屬性基礎(chǔ)上加上這句話,文本就會(huì)偏移,去掉就沒(méi)問(wèn)題.

以上所述是小編給大家介紹的解決iOS UITextField 編輯時(shí)文本偏移問(wèn)題,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI