所以我有一个自定义的clearButton为我的通用UITextField。我试图移动clearButton作为我的uitextfield的背景有一些设计的东西在右边。我需要把清除按钮推到150像素以上。
我尝试了所有这些组合,但它最终将清除按钮推离屏幕或使清除按钮非常宽,因此uitextfield中的文本停止方式太短。
1日试
UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect rect;
UIEdgeInsets contentInsets;
CGRect result;
[clearBtn setImage:[UIImage imageNamed:@"design.png"] forState:UIControlStateNormal];
rect = CGRectMake(0, 0, 45, 45);
contentInsets = UIEdgeInsetsMake(0, 50, 0, 0);
result = UIEdgeInsetsInsetRect(rect, contentInsets);
[clearBtn setFrame:result];
2日试
UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect rect;
UIEdgeInsets contentInsets;
CGRect result;
[clearBtn setImage:[UIImage imageNamed:@"design.png"] forState:UIControlStateNormal];
rect = CGRectMake(0, 0, 45, 45);
contentInsets = UIEdgeInsetsMake(0, 250, 0, 150);
result = UIEdgeInsetsInsetRect(rect, contentInsets);
[clearBtn setFrame:result];
一个更好的选择可能是子类化UITextField
并覆盖clearButtonRectForBounds:
方法。
- (CGRect)clearButtonRectForBounds:(CGRect)bounds {
CGRect rect = [super clearButtonRectForBounds:bounds];
rect.origin.x -= 150;
return rect;
}
将清除按钮从其正常位置向左移动150点。