亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

UITableViewCell中的iOS7上的自動布局約束問題

UITableViewCell中的iOS7上的自動布局約束問題

iOS
PIPIONE 2019-12-12 13:01:01
我正在以編程方式使用自動布局約束來布局自定義UITableView單元,并且正確定義了 tableView:heightForRowAtIndexPath:這是對iOS6的工作得很好,它的外觀罰款iOS7以及但是,當我在iOS7上運行該應用程序時,這是我在控制臺中看到的消息:Break on objc_exception_throw to catch this in the debugger.The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.2013-10-02 09:56:44.847 Vente-Exclusive[76306:a0b] 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:0xac4c5f0 V:|-(15)-[UIImageView:0xac47f50]   (Names: '|':UITableViewCellContentView:0xd93e850 )>",        "<NSLayoutConstraint:0xac43620 V:[UIImageView:0xac47f50(62)]>",        "<NSLayoutConstraint:0xac43650 V:[UIImageView:0xac47f50]-(>=0)-[UIView:0xac4d0f0]>",        "<NSLayoutConstraint:0xac43680 V:[UIView:0xac4d0f0(1)]>",        "<NSLayoutConstraint:0xac436b0 V:[UIView:0xac4d0f0]-(0)-|   (Names: '|':UITableViewCellContentView:0xd93e850 )>",)Will attempt to recover by breaking constraint <NSLayoutConstraint:0xac43650 V:[UIImageView:0xac47f50]-(>=0)-[UIView:0xac4d0f0]>而且的確有在該列表中,我不想約束之一:"<NSAutoresizingMaskLayoutConstraint:0xac6b120 h=--& v=--& V:[UITableViewCellContentView:0xd93e850(44)]>"而且我無法將的translatesAutoresizingMaskIntoConstraints屬性設置contentView為NO =>會弄亂整個單元格。44是默認的單元格高度,但是我在表格視圖委托中定義了我的自定義高度,那么為什么單元格contentView具有此約束?是什么原因造成的?在iOS6中這沒有發生,并且在iOS6和iOS7上一切都看起來很好。我的代碼很大,因此我不會在這里發布它,但是如果您需要它,可以隨時請求一個pastebin。要指定我的操作方式,有關單元格初始化:我創建了所有標簽,按鈕等我將其translatesAutoresizingMaskIntoConstraints財產設置為“否”我將它們添加為contentView單元格的子視圖我在 contentView我也很想了解為什么僅在iOS7上會發生這種情況。
查看完整描述

3 回答

?
largeQ

TA貢獻2039條經驗 獲得超8個贊

我也遇到了這個問題。似乎在layoutSubviews調用contentView的框架之前,它不會得到更新, 但是單元格的框架會更早更新,而在{0, 0, 320, 44}評估約束時將contentView的框架設置為。


詳細查看contentView之后,似乎不再設置autoresizingMask。


在約束視圖之前設置autoresizingMask可以解決此問題:


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];

    if (self)

    {

        self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;

        [self loadViews];

        [self constrainViews];

    }

    return self;

}


查看完整回答
反對 回復 2019-12-12
?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

顯然,使用iOS 8 SDK的iOS 7上的UITableViewCell和UICollectionViewCell出了問題。


像這樣重用單元格時,可以更新單元格的contentView:


對于靜態UITableViewController:


#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED

#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];


    if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1)

    {

        cell.contentView.frame = cell.bounds;

        cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleTopMargin |UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;

    }


    //your code goes here


    return cell;

}


#endif

#endif

由于靜態表視圖控制器易碎,如果實現某些數據源或deletegate方法,則很容易損壞-有檢查將確保僅在iOS 7上編譯和運行此代碼


它與標準動態UITableViewController類似:


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *cellID = @"CellID";


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];


    if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1)

    {

        cell.contentView.frame = cell.bounds;

        cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleTopMargin |UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;

    }

    //your code goes here       

    return cell;

}

對于這種情況,我們不需要額外的編譯檢查,因為需要實現此方法。


這兩種情況以及UICollectionViewCell的想法都是相同的,就像在此線程中所評論的:僅在iOS 7上運行時,才會在Storyboard原型單元(Xcode 6,iOS 8 SDK)中自動調整UICollectionViewCell contentView的框架大小


查看完整回答
反對 回復 2019-12-12
  • 3 回答
  • 0 關注
  • 672 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號