簡體   English   中英

布局不明確-我需要為兩個並排視圖添加哪些其他約束?

[英]Ambiguous Layout - What Additional Constraint(s) Do I Need to Add for Two Side by Side Views?

我有一個從導航欄向下滑動的視圖-允許用戶更改標題名稱。 我正在以編程方式使用自動布局。

我的目標是 :標簽占據所需的水平空間(例如,由於本地化而發生的變化),並將其固定在其超級視圖的左側(帶有邊距)。 文本字段將占用其余空間,而不管其內容如何(並且在其和標簽之間有一個空白,在右邊有一個空白)。

問題UILabelUITextField布局都不明確。 除非我的約束是完全錯誤的,否則我需要添加其他約束...我只是不確定要添加什么。 我已經在水平訪問中嘗試過setContentCompressionResistancePrioritysetContentHuggingPriority ,但是還沒有做到這一點。

我想念什么?

根據以下代碼,以下圖像沒有適當的“ 擁抱”或“ 壓縮”優先級

一直看起來如何: 在此處輸入圖片說明

編輯為短名稱可能導致以下情況: 在此處輸入圖片說明

編輯為長名稱可能會導致以下情況: 在此處輸入圖片說明

這是代碼:

_renameView = [UIView new];
_renameView.translatesAutoresizingMaskIntoConstraints = NO;
_renameView.backgroundColor = _styles.navbarLightGreyTrans;
[self.view addSubview:_renameView];

_renameTextField = [UITextField new];
_renameTextField.translatesAutoresizingMaskIntoConstraints = NO;
_renameTextField.delegate = self;
_renameTextField.clipsToBounds = YES;
_renameTextField.backgroundColor = [UIColor whiteColor];
_renameTextField.layer.cornerRadius = 4.0f;
_renameTextField.layer.borderWidth = 0.7f;
_renameTextField.layer.borderColor = [_styles.greyBorder CGColor];
_renameTextField.font = [UIFont systemFontOfSize:16.0f];
[_renameTextField setReturnKeyType:UIReturnKeyDone];
[_renameView addSubview:_renameTextField];

UIView* separator = [UIView new];
separator.translatesAutoresizingMaskIntoConstraints = NO;
separator.backgroundColor = _styles.greyBorder;
[_renameView addSubview:separator];

UILabel* label = [UILabel new];
label.text = @"Name:";
label.font = [UIFont systemFontOfSize:16.0f];
label.translatesAutoresizingMaskIntoConstraints = NO;
[_renameView addSubview:label];

約束:

constraints = [NSLayoutConstraint
               constraintsWithVisualFormat:@"H:|[_renameView]|"
               options:0
               metrics:nil
               views:views];

[self.view addConstraints:constraints];

constraints = [NSLayoutConstraint
               constraintsWithVisualFormat:@"H:|[separator]|"
               options:0
               metrics:nil
               views:views];

[_renameView addConstraints:constraints];

constraints = [NSLayoutConstraint
               constraintsWithVisualFormat:@"H:|-10-[label]-[_renameTextField]-10-|"
               options:NSLayoutFormatAlignAllCenterY
               metrics:nil
               views:views];

[_renameView addConstraints:constraints];

constraints = [NSLayoutConstraint
               constraintsWithVisualFormat:@"V:|-7-[_renameTextField(==36)]-7-[separator(==0.72)]|"
               options:0
               metrics:nil
               views:views];

[_renameView addConstraints:constraints];

更新

歧義布局的日志:

|   |   |   |   |   *<UIView:0x10c57d710>
|   |   |   |   |   |   *<UITextField:0x10c57d990> - AMBIGUOUS LAYOUT
|   |   |   |   |   |   |   <UIFieldEditor:0x10c60a150>
|   |   |   |   |   |   |   |   <_UIFieldEditorContentView:0x10c60a710>
|   |   |   |   |   |   |   |   |   <UITextSelectionView:0x10c038850>
|   |   |   |   |   |   |   |   |   |   <UIView:0x10c034730>
|   |   |   |   |   |   |   |   <UIImageView:0x10c60f790>
|   |   |   |   |   |   |   |   <UIImageView:0x10c60fad0>
|   |   |   |   |   |   |   <UIView:0x10c582620>
|   |   |   |   |   |   *<UIView:0x10c582700>
|   |   |   |   |   |   *<UILabel:0x10c5827e0> - AMBIGUOUS LAYOUT
|   |   |   |   |   *<_UILayoutGuide:0x10c582b50> - AMBIGUOUS LAYOUT

標簽和文本字段之間在水平軸上的空間分布不明確。 嘗試設置標簽的內容優先級。 我省略了垂直約束。

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label]-[textField]-|"
                                                              options:0
                                                              metrics:nil
                                                                views:viewsDictionary]];

[label setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[label setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

我已經通過固定UILabel的寬度刪除了模棱兩可的布局。

constraints = [NSLayoutConstraint
               constraintsWithVisualFormat:@"H:|-10-[label(==50)]-[_renameTextField]-10-|"
               options:NSLayoutFormatAlignAllCenterY
               metrics:nil
               views:views];

[_renameView addConstraints:constraints];

無論內容多長或短,都可以在UITextField 當然,我不得不對標簽的寬度進行硬編碼,這就是我要避免的事情...

在此處輸入圖片說明

暫無
暫無

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

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