2015-11-09 44 views
0

我想以編程方式將圖像視圖(複選標記)添加爲一個文本字段的子視圖。由於我在所有視圖中使用自動佈局,因此我想使用約束來定位此子視圖。下面是代碼:iOS - 以自動佈局在UITextField中以編程方式定位UIImageView

class UIFormTextField: UITextField { 

    lazy var checkMarkView = UIImageView(image: UIImage(named: "BarButtonItemCheck")) 

    func setCheckMarkView() { 
     self.addSubview(checkMarkView) 
     self.addConstraints([ 
      NSLayoutConstraint(item: checkMarkView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: CGFloat(1), constant: CGFloat(0)), 
      NSLayoutConstraint(item: checkMarkView, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: CGFloat(1), constant: -UIStyle.Size.standardHorizontalSpacing) 
      ]) 
    } 
} 

我在viewDidLoad中函數中調用setCheckMarkView但它不工作。我收到了一個警告:「可能下面列表中至少有一個限制是你不想要的限制。」

下面是列表,但我不明白爲什麼其他約束會干擾新視圖。

"<NSAutoresizingMaskLayoutConstraint:0x15084acf0 h=--& v=--& UIImageView:0x15089f4d0.midX == + 12>", 
"<NSAutoresizingMaskLayoutConstraint:0x14f691d70 h=--& v=--& H:[UIImageView:0x15089f4d0(24)]>", 
"<NSLayoutConstraint:0x1509c54e0 H:[Bruce.UIFormTextField:0x150895520]-(0)-| (Names: '|':Bruce.UIForm:0x150998430)>", 
"<NSLayoutConstraint:0x1509a6120 H:|-(0)-[Bruce.UIFormTextField:0x150895520] (Names: '|':Bruce.UIForm:0x150998430)>", 
"<NSLayoutConstraint:0x14f593e20 H:[Bruce.UIForm:0x150998430]-(0)-| (Names: '|':UIView:0x1509c92d0)>", 
"<NSLayoutConstraint:0x150950f10 H:|-(0)-[Bruce.UIForm:0x150998430] (Names: '|':UIView:0x1509c92d0)>", 
"<NSLayoutConstraint:0x14f6143b0 UIView:0x1509c92d0.width == UIView:0x15094e8f0.width>", 
"<NSLayoutConstraint:0xUIImageView:0x15089f4d0.trailing == Bruce.UIFormTextField:0x150895520.trailing - 20>", 
"<NSLayoutConstraint:0x150896350 'UIView-Encapsulated-Layout-Width' H:[UIView:0x15094e8f0(320)]>" 

感謝您的幫助,

+0

我沒有清楚你想要達到的目標。我個人認爲,劃分這樣一個重要組件並不是一個好主意。另一種解決方案是組合你的組件,從一個帶有圖像視圖的文本字段的視圖構建,作爲兄弟 – Andrea

+0

Hello Andrea。我的目標是在用戶輸入有效時顯示覆選標記,並在不存在時將其隱藏。我爲textfield子類實現了幾個方法,如「nextResponder」或「setInputAccessoryView」。我已經設法構建了一個包含2個視圖(TextField和ImageView)的組件,但是我必須將ImageView放在文本框的上方(它不透明)。或者在這種情況下,圖像阻止點擊文本字段。因此,我正在尋找一個更清晰的子視圖解決方案。 – Helmikku

回答

3

的問題是,你需要禁用對的ImageView的translatesAutoresizingMaskIntoConstraints財產。這就是爲什麼你看到「NSAutoresizingMaskLayoutConstraint」問題的制約

更新的代碼:

lazy var checkMarkView: UIImageView = { 
    let imageView = UIImageView(image: UIImage(named: "BarButtonItemCheck")) 
    imageView.translatesAutoresizingMaskIntoConstraints = false 
    return imageView 
}() 

編輯: 你實際上並不需要禁用,你聲明checkMarkView財產。你也可以在你的函數「setCheckMarkView」中做到這一點,即

func setCheckMarkView() { 
    checkMarkView.translatesAutoresizingMaskIntoConstraints = false 
    self.addSubview(checkMarkView) 
    self.addConstraints([ 
     NSLayoutConstraint(item: checkMarkView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: CGFloat(1), constant: CGFloat(0)), 
     NSLayoutConstraint(item: checkMarkView, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: CGFloat(1), constant: -UIStyle.Size.standardHorizontalSpacing) 
     ]) 
} 
+0

非常感謝@Lneuner。 – Helmikku

相關問題