rightView UITextField的位置

有没有办法调整UITextField上rightView的位置? 我尝试在视图上设置框架(设置为rightView),但它没有改变任何东西。

我想避免制作两个视图,一个是rightView,另一个是rightView的子视图,如果可能的话,我会更改子视图的位置。

右侧叠加视图放置在接收器的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 ; } 

@Puneet Sharma的答案很棒,但这需要创建一个可以inheritanceUITextField的类,而我所做的却是创建一个充当填充的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 ,然后给出了有填充但没有填充的错觉。