2013-10-09 2 views
0

프로그래밍 방식으로 테이블 뷰에 UITextView를 추가했습니다. 세로 및 가로 모드에서 동일하게 보이도록 구속 조건을 추가하고 싶습니다. 다음 코드 (C#, MonoTouch)가 있지만 작동하지 않습니다. titleTxt는 셀에 추가 된 간단한 텍스트 필드입니다.MonoTouch 프로그래밍 방식으로 UITextField에 제약 조건을 추가하십시오.

var rightSpaceConstraint = NSLayoutConstraint.Create (titleTxt, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, null, NSLayoutAttribute.Right, 1.0f, 20f); 
var leftSpaceConstraint = NSLayoutConstraint.Create (titleTxt, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, null, NSLayoutAttribute.Left, 1.0f, 20f); 

titleTxt.AddConstraints (new NSLayoutConstraint[] {rightSpaceConstraint, leftSpaceConstraint}); 

왼쪽 및 오른쪽 공백을 설정하는 데 제약 조건을 어떻게 추가해야합니까?

답변

1

titleTxt에 제약 조건을 추가하는 대신 표 셀에 제약 조건을 추가해야합니다. 또한 TranslatesAutoresizingMaskIntoConstraints 플래그를 false로 설정해야합니다. 아래 코드는 정상적으로 작동합니다.

 UILabel titleLbl = new UILabel(); 
     titleLbl.Frame = new RectangleF (20,5,260,30); 
     titleLbl.Text = "Please enter username"; 
     titleLbl.BackgroundColor = UIColor.Clear; 
     titleLbl.Font = UIFont.SystemFontOfSize (13); 
     titleLbl.TextAlignment = UITextAlignment.Center; 

     titleLbl.AutoresizingMask = UIViewAutoresizing.FlexibleWidth; 
     titleLbl.TranslatesAutoresizingMaskIntoConstraints = false; 

     cell.ContentView.AddSubview (titleLbl); 

     var titleLblRightSpaceConstraint = NSLayoutConstraint.Create (titleLbl, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, cell.ContentView, NSLayoutAttribute.Right, 1.0f, -20f); 
     var titleLblLeftSpaceConstraint = NSLayoutConstraint.Create (titleLbl, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, cell.ContentView, NSLayoutAttribute.Left, 1.0f, 0f); 

     cell.ContentView.AddConstraints (new NSLayoutConstraint[] {titleLblRightSpaceConstraint, titleLblLeftSpaceConstraint }); 
+0

자동 레이아웃과 제약 조건을 사용하는 경우 프레임을 제공해야합니까? – SARATH

관련 문제