一個更簡單的解決方案,這需要的contentMode
優點:
arrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"down_arrow"]];
arrow.frame = CGRectMake(0.0, 0.0, arrow.image.size.width+10.0, arrow.image.size.height);
arrow.contentMode = UIViewContentModeCenter;
textField.rightView = arrow
textField.rightViewMode = UITextFieldViewModeAlways;
在夫特2,
let arrow = UIImageView(image: UIImage(named: "down_arrow"))
arrow.frame = CGRectMake(0.0, 0.0, arrow.image.size.width+10.0, arrow.image.size.height);
arrow.contentMode = UIViewContentMode.Center
self.textField.rightView = arrow
self.textField.rightViewMode = UITextFieldViewMode.Always
在夫特3,
let arrow = UIImageView(image: UIImage(named: "arrowDrop"))
if let size = arrow.image?.size {
arrow.frame = CGRect(x: 0.0, y: 0.0, width: size.width + 10.0, height: size.height)
}
arrow.contentMode = UIViewContentMode.center
self.textField.rightView = arrow
self.textField.rightViewMode = UITextFieldViewMode.always
的可能重複[插圖文字爲UITextField?](http://stackoverflow.com/questions/2694411/text-inset-for-uitextfield) – Zorayr
無,這是詢問如何在文本字段中縮進顯示爲左視圖的圖像。不縮進文本本身。試試他的代碼,你會發現將leftView設置爲一個圖像可以將圖像放置在文本字段的左邊緣,而不需要填充。它看起來很醜。 – stone