簡體   English   中英

在iOS 7中更新約束后得到“無法同時滿足約束”

[英]Getting “Unable to simultaneously satisfy constraints” after updating constraint in iOS 7

我在表的標題中有一個UITableViewController,帶有一個自定義視圖,其中包含一些子視圖和控件。 這些子視圖中的每個子視圖都是一個自定義子視圖(使用自定義子視圖通過IBInspectable繪制拐角半徑),每個子視圖都對頂部,底部,前導和尾隨空間(均設置為8)以及高度(設置為60, 80或100,具體取決於每個子視圖)。

這些子視圖約束之一可以在運行時根據用戶交互以編程方式進行修改。 為此,我創建了兩個方法:

- (void)showSearchTypeTermField:(BOOL)animated
{
    self.searchTypeHeightConstraint.identifier = @"Height 100";

    [self.searchTypeHeightConstraint setConstant:100.0];

    if (animated) {
        [self.tableView.tableHeaderView layoutIfNeeded];
        [UIView animateWithDuration:0.3 animations:^{
            [self.tableView.tableHeaderView layoutIfNeeded];
        }];
    } else {
        [self.tableView.tableHeaderView layoutIfNeeded];
    }

    self.searchTypeTermField.hidden = NO;
    self.searchTypeTermField.text = @"";

    [self.tableView.tableHeaderView systemLayoutSizeFittingSize:UILayoutFittingExpandedSize];
}

- (void)hideSearchTypeTermField:(BOOL)animated
{
    self.searchTypeHeightConstraint.identifier = @"Height 60";

    [self.tableView.tableHeaderView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];

    [self.searchTypeHeightConstraint setConstant:60.0];

    if (animated) {
        [self.tableView.tableHeaderView layoutIfNeeded];
        [UIView animateWithDuration:0.3 animations:^{
            [self.tableView.tableHeaderView layoutIfNeeded];
        }];
    } else {
        [self.tableView.tableHeaderView layoutIfNeeded];
    }

    self.searchTypeTermField.hidden = YES;

    [self.tableView.tableHeaderView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
}

因為所有這些子視圖都在表標題視圖中,所以每次更改約束時,標題都應根據情況擴展或壓縮,這就是為什么我使用self.tableView.tableHeaderView systemLayoutSizeFittingSize

問題是,當我第一次在viewdidLoad上調用[self hideSearchTypeTermField:NO]時,盡管在視覺上看起來一切正常,但我遇到了此錯誤:

Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7bf504e0 V:[SeachFormBackgroundView:0x7bea3ab0(60)]>",
    "<NSLayoutConstraint:0x7bf51760 V:[SeachFormBackgroundView:0x7bf50690(60)]>",
    "<NSLayoutConstraint:0x7bf53390 'Height 60' V:[SeachFormBackgroundView:0x7bf518e0(60)]>",
    "<NSLayoutConstraint:0x7bf53ec0 V:[SeachFormBackgroundView:0x7bf535a0(80)]>",
    "<NSLayoutConstraint:0x7bf54a80 V:[SeachFormBackgroundView:0x7bf54180(80)]>",
    "<NSLayoutConstraint:0x7bf54eb0 V:|-(8)-[SeachFormBackgroundView:0x7bea3ab0]   (Names: '|':UIView:0x7bea3a10 )>",
    "<NSLayoutConstraint:0x7bf54f40 V:[SeachFormBackgroundView:0x7bea3ab0]-(8)-[SeachFormBackgroundView:0x7bf50690]>",
    "<NSLayoutConstraint:0x7bf54f70 V:[SeachFormBackgroundView:0x7bf50690]-(8)-[SeachFormBackgroundView:0x7bf518e0]>",
    "<NSLayoutConstraint:0x7bf55090 V:[SeachFormBackgroundView:0x7bf518e0]-(8)-[SeachFormBackgroundView:0x7bf535a0]>",
    "<NSLayoutConstraint:0x7bf550f0 V:[SeachFormBackgroundView:0x7bf54180]-(8)-|   (Names: '|':UIView:0x7bea3a10 )>",
    "<NSLayoutConstraint:0x7bf55180 V:[SeachFormBackgroundView:0x7bf535a0]-(8)-[SeachFormBackgroundView:0x7bf54180]>",
    "<NSAutoresizingMaskLayoutConstraint:0x7c149fc0 h=--& v=--& V:[UIView:0x7bea3a10(428)]>"
)

我真的迷失了這個問題。 我做錯了什么?

謝謝。

如果嘗試為SeachFormBackgroundView實例(60 + 60 + 60 + 80 + 80 + 8 * 6 = 388)及其分隔符添加高度,您會發現它們不等於父UIView高度(428)。這就是原因您會收到此消息。

您必須調整約束以使它們加起來達到父視圖的高度,或者調整父對象的大小以使其大小與子約束匹配。

您不需要現在擁有的所有約束。 由於您要為所有子視圖和間距約束設置特定的高度,因此只需要將它們錨定到超級視圖的頂部或底部即可。

編輯:

您不需要這兩個約束: V:|-(8)-[SeachFormBackgroundView:0x7bea3ab0]V:[SeachFormBackgroundView:0x7bf54180]-(8)-| 第一個將子視圖錨定到超級視圖的頂部,第二個將子視圖錨定到超級視圖的底部。 刪除其中之一(我希望是最下面的一個),視圖將就位,而不會引發任何AutoLayout異常。

設置標題視圖時,表格視圖會讀取其大小並為該高度設置約束。 更新標題視圖本身的約束不會導致對該高度的重新評估。 您應該刪除標題視圖,更新其約束,進行布局,然后再次添加標題視圖。

暫無
暫無

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

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