2016-05-02 10 views
0

UIViewのサブクラスを持っています。その中にUITextFieldがあり、プレースホルダテキストを少し右に移動したいと考えています。私はサブビューの位置とサイズを設定するのにNSLayoutConstraintを使用しています。私はUIViewを作成し、それをleftViewUITextFieldとして追加しましたが、「ビュー階層が制約のために準備されていません」とクラッシュします。以下は私のコードです:SwiftのNSLayoutConstraintを使用してUITextField内のパディングを追加する方法は?

class MasterView: UIView { 

let searchTextField = UITextField() 
let paddingView = UIView() 

override init(frame: CGRect) { 
    super.init(frame: frame) 

    searchTextField.placeholder = "Search videos" 
    searchTextField.translatesAutoresizingMaskIntoConstraints = false 
    paddingView.translatesAutoresizingMaskIntoConstraints = false 
    searchTextField.leftView = paddingView 
    searchTextField.leftViewMode = UITextFieldViewMode.Always 

    self.addSubview(searchTextField) 

} 

// Gets called when using storyboard 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 

override func layoutSubviews() { 
    self.addConstraint(NSLayoutConstraint(item: searchTextField, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.TopMargin, multiplier: 1.0, constant: 20)) 

    self.addConstraint(NSLayoutConstraint(item: searchTextField, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Height, multiplier: 0.12, constant: 0)) 

    self.addConstraint(NSLayoutConstraint(item: searchTextField, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0)) 

    self.addConstraint(NSLayoutConstraint(item: searchTextField, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0)) 

    self.addConstraint(NSLayoutConstraint(item: paddingView, attribute: NSLayoutAttribute.LeadingMargin, relatedBy: NSLayoutRelation.Equal, toItem: searchTextField, attribute: NSLayoutAttribute.LeadingMargin, multiplier: 1.0, constant: 0)) 

    self.addConstraint(NSLayoutConstraint(item: paddingView, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: searchTextField, attribute: NSLayoutAttribute.TopMargin, multiplier: 1.0, constant: 0)) 

    self.addConstraint(NSLayoutConstraint(item: paddingView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: searchTextField, attribute: NSLayoutAttribute.Width, multiplier: 0.12, constant: 0)) 

    self.addConstraint(NSLayoutConstraint(item: paddingView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: searchTextField, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 0)) 

} 

}

答えて

0
let paddingForFirst = UIView(frame: CGRectMake(0, 0, 5, self.Email_Txt.frame.size.height-5)) 
Email_Txt.leftView = paddingForFirst 
Email_Txt.leftViewMode = UITextFieldViewMode .Always 
+0

から取ら

@IBDesignable class FormTextField: UITextField { @IBInspectable var paddingLeft: CGFloat = 0 @IBInspectable var paddingRight: CGFloat = 0 override func textRect(forBounds bounds: CGRect) -> CGRect { return CGRect(x: bounds.origin.x + paddingLeft, y: bounds.origin.y, width: bounds.size.width - paddingLeft - paddingRight, height: bounds.size.height) } override func editingRect(forBounds bounds: CGRect) -> CGRect { return textRect(forBounds: bounds) } 

}

。どうしたらいいのですか? – Nirmal

0

それはあなたが

override func viewDidLoad() { 
super.viewDidLoad() 
let paddingView = UIView(frame: CGRectMake(0, 0, 15, self.myTextField.frame.height)) 
myTextField.leftView = paddingView 
myTextField.leftViewMode = UITextFieldViewMode.Always 
} 
0

直下書くのに役立ちます。このコードを試してください: - SWIFT 2.1.1

let tempView = UIView(frame:CGRectMake(6, 0, 22, 26)) 
tempView.backgroundColor = UIColor.clearColor() 
searchTextField.leftView?.frame = CGRectMake(0, 0, tempView.frame.size.width, tempView.frame.height) 

searchTextField.leftViewMode = UITextFieldViewMode.Always 
searchTextField.leftView = tempView 
0

yon shパディングビューを設定したり、制約を与えたりしないでください。パディングビューのすべての制約を削除します。 textfieldの左のビューですので、テキストフィールドの制約に従って自動的に調整されます。パディングのフレームを設定すると、パディングとして期待するスペースが表示されます。

希望これは:)

4

はUITextFieldのカスタムクラスを作成し、そのクラスで、この2つの方式を実装し、カスタムテキストフィールドとして、あなたのテキストフィールドを作るのに役立ちます。

class customTextField: UITextField { 

override func textRectForBounds(bounds: CGRect) -> CGRect { 
    return super.textRectForBounds(UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(0, 5, 0, 0))) 
} 

override func editingRectForBounds(bounds: CGRect) -> CGRect { 
    return super.editingRectForBounds(UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(0, 5, 0, 0))) 
} 
} 
0

あなたはこのエラーに直面しているpaddingViewは、左/右のビューとして追加され、任意の制約を必要としないので、を「ビュー階層は、制約のために用意されていません」。

注:左/右ビューとして追加されたビューに制約を与える必要はありません。UITextField。だから、フレームをpaddingViewに設定し、それをleftViewに割り当てるだけです。 paddingViewに制約を与える必要はありません。この問題の

8

適切な解決策:私はパディングビューを設定するためのNSLayoutConstraintを使用したいhere

+0

すごく大変ありがとう!これは正解とマークする必要があります。 – GameDev

関連する問題