美文网首页iOS 记录小功能
iOS 13 左右视图错位 UITextField 添加右侧按钮

iOS 13 左右视图错位 UITextField 添加右侧按钮

作者: RXX_xx | 来源:发表于2020-08-31 19:25 被阅读0次

iOS 自定义UITextField1、在TextField的右侧添加view+手势 左侧和右侧同理2、在TextField在键盘上添加‘完成’按钮

    只是作备份记录

NHTextFieldKeyWord.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@protocol NHTextFieldKeyWordDelegate <NSObject>

- (void)doneBtnWithTextFile:(UITextField*)textField;

- (void)addBtnActionWithTextFile:(UITextField *)textField;

@end

@interface NHTextFieldKeyWord : UITextField

@property (nonatomic, weak) id<NHTextFieldKeyWordDelegate> NHKeyWordDelegate;

@end

NS_ASSUME_NONNULL_END


NHTextFieldKeyWord.m

#import "NHTextFieldKeyWord.h"

#import "JMTool.h"

@interface NHTextFieldKeyWord ()<UITextFieldDelegate>

@end

@implementation NHTextFieldKeyWord

- (void)drawRect:(CGRect)rect {

     [self setDefaultInputAccessoryViewWithTarget:self action:@selector(numberFieldCancle)];

     [self setAddBtnWithTarget:selfaction:@selector(addBtnTextField)];

 }

#pragma mark 在TextField的右侧添加view+手势

- (void)setAddBtnWithTarget:(id)target action:(SEL)action{

    UIView*addView = [[UIViewalloc]init];

    addView.backgroundColor=RGB(245,245,245);

    addView.layer.masksToBounds = YES;

    addView.layer.cornerRadius=3.0f;

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:target action:action];

    addView.userInteractionEnabled = YES;

    [addViewaddGestureRecognizer:tap];

    UILabel*addText = [[UILabelalloc]init];

    addText.text=@"添加";

    addText.textColor=RGB(59,192,194);

    addText.font= [UIFontsystemFontOfSize:13.0f];

    [addViewaddSubview:addText];

    CGSizemaxSize =CGSizeMake(100,13);

    CGSizeexpSize = [addTextsizeThatFits:maxSize];

    [addTextmas_makeConstraints:^(MASConstraintMaker*make) {

        make.right.mas_equalTo(-15*coefficient);

        make.width.mas_equalTo(expSize.width);

        make.height.mas_equalTo(expSize.height);

        make.centerY.mas_equalTo(addView.center);

    }];

    addView.frame=CGRectMake(self.frame.size.width- expSize.width-30*coefficient,0, expSize.width+30*coefficient,self.frame.size.height);

    self.rightView= addView;

    self.rightViewMode = UITextFieldViewModeAlways;

    self.rightView.hidden = YES;

self.rightView.frame = [self rightViewRectForBounds:CGRectMake(0, 0, expSize.width+30*coefficient, self.frame.size.height)];

}

- (CGRect)rightViewRectForBounds:(CGRect)bounds {

    CGRectrightViewRect = [superrightViewRectForBounds:bounds];

    if(@available(iOS13.0, *)) {

        CGFloat left = self.frame.size.width - self.rightView.frame.size.width;

        CGFloat top = round((self.frame.size.height - self.rightView.frame.size.height)/2.0);

        rightViewRect =CGRectMake( left, top,self.rightView.frame.size.width,self.rightView.frame.size.height);

    }else{

    }

    returnrightViewRect;

}

#pragma mark 在TextField在键盘上添加‘完成’按钮

- (void)setDefaultInputAccessoryViewWithTarget:(id)target action:(SEL) action{

    UIToolbar*toolBar = [UIToolbarnew];

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    UIBarButtonItem*doneBtn = [[UIBarButtonItemalloc]initWithTitle:@"完成"style:UIBarButtonItemStyleDonetarget:targetaction:action];

    doneBtn.tintColor=RGB(0,122,255);

    NSArray*items =@[flexSpace,doneBtn];

    toolBar.items= items;

    [toolBarsizeToFit];

    self.inputAccessoryView = toolBar;

}

- (void)numberFieldCancle{

    [self resignFirstResponder];  //收起键盘

    if(self.NHKeyWordDelegate&& [self.NHKeyWordDelegaterespondsToSelector:@selector(doneBtnWithTextFile:)]) {

        [self.NHKeyWordDelegate doneBtnWithTextFile:self];

    }

}

- (void)addBtnTextField{

    [self resignFirstResponder];  //收起键盘

    if(self.NHKeyWordDelegate&& [self.NHKeyWordDelegaterespondsToSelector:@selector(addBtnActionWithTextFile:)]) {

        [self.NHKeyWordDelegate addBtnActionWithTextFile:self];

    }

}

@end


引用

NHTextFieldKeyWord *textField = [[NHTextFieldKeyWord alloc] init];

textField.NHKeyWordDelegate = self;

其他设置按继承类使用

代理方法

完成按钮点击

- (void)doneBtnWithTextFile:(UITextField *)textField{}

右侧添加按钮点击

- (void)addBtnActionWithTextFile:(UITextField *)textField{}


引用了 其他博主标题为‘iOS 13 ,UITextField rightView leftView 错位显示,解决办法’的文章 感谢

谨作备份记录

相关文章

网友评论

    本文标题:iOS 13 左右视图错位 UITextField 添加右侧按钮

    本文链接:https://www.haomeiwen.com/subject/unccdttx.html