簡體   English   中英

iOS ObjectiveC-旋轉回縱向后,UIView無法正確顯示

[英]iOS ObjectiveC - UIView not show correctly after rotating BACK to Portrait

我是目標C的新手,在首次旋轉為橫向后旋轉回縱向時,似乎無法在tableViewCell中設置正確的UIView。

我檢查了幾篇相關的文章,但似乎沒有任何解決方法。 當我打印寬度時,盡管輸出不正確,但可以得到預期的結果。

我創建了一個UIView“ lineView”,以在tableView的單元格之間添加自定義行(由於不同部分中的一些分隔符問題,所以這是必需的)。

這是代碼的一部分:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"anIdentifier" forIndexPath:indexPath];    

     ... Content of the cell

     UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10, 42,tableView.bounds.size.width-20, 0.7);    
     lineView.backgroundColor = [UIColor blackColor];

     [cell.contentView addSubview:lineView];

     return cell;
}

當我運行整個程序時,我得到以下輸出(從左到右:不旋轉,旋轉到橫向,旋轉回縱向):

在此處輸入圖片說明 在此處輸入圖片說明 在此處輸入圖片說明

圖1和2中的分隔線是正確的,但是當我旋轉回橫向時,右插圖消失了。 可能有一個簡單的解決方案,但我的原始技能讓我失望了。

我想念什么?

如果有人遇到類似問題,這是我解決問題的方法(感謝@codedad的評論):

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

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"anIdentifier" forIndexPath:indexPath];    

     ... Content of the cell

    UIView *lineView = [[UIView alloc] init];
    lineView.translatesAutoresizingMaskIntoConstraints=NO;

    [cell.contentView addSubview:lineView];

    NSArray *verticalConstraints =
      [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-41.5-[lineView(0.8)]"
                                            options: 0
                                            metrics:nil
                                            views:NSDictionaryOfVariableBindings(lineView)];


    NSArray *horizontalConstraints =
      [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[lineView(>=50)]-20-|"
                                        options: 0
                                        metrics:nil
                                          views:NSDictionaryOfVariableBindings(lineView)];

    [cell.contentView addConstraints:verticalConstraints];
    [cell.contentView addConstraints:horizontalConstraints];
}

您也可以使用'constraintsWithItem'。 我嘗試了兩種方法,它們都工作正常( https://developer.apple.com/library/prerelease/ios/documentation/AppKit/Reference/NSLayoutConstraint_Class/index.html )。

我仍然不知道為什么使用“框架”會引起最初的問題,但無論如何,使用NSLayoutconstrainClass似乎是一種更安全,更可靠的方法。

暫無
暫無

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

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