簡體   English   中英

rightView UITextField 的位置

[英]Position of 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 的回答很好,但這需要創建一個繼承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 ,它具有透明的彩色背景,然后給人一種有填充而沒有填充的錯覺。

您可以使用的正確填充

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 = imageview

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM