有没有办法调整UITextField上rightView的位置?我试着在视图上设置框架(设置为rightView),但是没有改变任何东西。
我想避免创建两个视图,一个作为rightView,另一个作为rightView的子视图,如果可能的话,我会改变子视图的位置。
发布于 2013-08-22 13:03:54
右侧覆盖视图被放置在由接收器的rightViewRectForBounds:方法返回的矩形中。
所以我建议你子类化UITextField并覆盖这个方法,如下所示:
@interface CustomTextField: UITextField
@end
@implementation CustomTextField
// override rightViewRectForBounds method:
- (CGRect)rightViewRectForBounds:(CGRect)bounds{
CGRect rightBounds = CGRectMake(bounds.origin.x + 10, 0, 30, 44);
return rightBounds ;
}发布于 2017-07-04 17:46:23
@Puneet Sharma的回答很好,但这就需要创建一个类来作为UITextField的子类,而我所做的是创建一个充当填充的UIView。
此代码无需子类即可工作
以下是我的代码,尽管它是用Swift 3编写的
// this is the view I want to see on the rightView
let checkImageView = UIImageView(image: UIImage(named: "check24.png"))
checkImageView.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
checkImageView.curveEdges(12)
checkImageView.contentMode = .scaleAspectFit
// declare how much padding I want to have
let padding: CGFloat = 6
// create the view that would act as the padding
let rightView = UIView(frame: CGRect(
x: 0, y: 0, // keep this as 0, 0
width: checkImageView.frame.width + padding, // add the padding
height: checkImageView.frame.height))
rightView.addSubview(checkImageView)
// set the rightView UIView as the textField's rightView
self.textField.rightViewMode = .whileEditing
self.textField.rightView = rightView这里发生的事情是,rightView是一个UIView,它有一个透明的彩色背景,然后给人一种有填充的错觉,而实际上没有。
发布于 2019-11-20 14:48:05
您可以将右填充用作
let imageview = UIImageView(image: UIImage(named: "image name"))
imageview.contentMode = .center
let rightPadding: CGFloat = 14 //--- change right padding
imageview.frame = CGRect(x: 0, y: 0, width: imageview.frame.size.width + rightPadding , height:imageview.frame.size.height)
textField.rightViewMode = .always
textFieldd.rightView = imageviewhttps://stackoverflow.com/questions/18371053
复制相似问题