UITextfiled子视图被剪切

时间:2021-05-22 08:20:24

一般情况下,如果在view1上面添加了view2,但是view2超出了view1,也会在屏幕上面显示超出的部分

例如:

UIButton *button =[[UIButton alloc]initWithFrame:CGRectMake(100, 400, 120, 30)];
[self.view addSubview:button];
UIButton * btn1 = [[UIButton alloc]initWithFrame:CGRectMake(button.frame.size.width-5, -5, 10, 10)];
btn1.backgroundColor = [UIColor redColor];
[button addSubview:btn1];
button.backgroundColor = [UIColor grayColor];
显示结果如下(图一)

但是如果是UItextfiled,那么就会默认的剪切掉超出textfiled的部分

例如:

  UITextField * filed = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 120, 30)];
    [self.view addSubview:filed];
    filed.borderStyle = UITextBorderStyleRoundedRect;
    UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(filed.frame.size.width-5, -5, 10, 10)];
    [filed addSubview:btn];
    btn.backgroundColor = [UIColor redColor];
    self.view.backgroundColor = [UIColor whiteColor];

显示结果图二

UITextfiled子视图被剪切

这个造成的原因是uitextfiled会默认的在layer层给超出父控件的给剪切掉,解决方法如下

filed.layer.masksToBounds = NO

显示结果如图三