API ที่ถูกต้องในการใช้งานจะUIView systemLayoutSizeFittingSize:ผ่านอย่างใดอย่างหนึ่งหรือUILayoutFittingCompressedSizeUILayoutFittingExpandedSize
สำหรับการUIViewใช้การชำระอัตโนมัติแบบปกติสิ่งนี้ควรทำงานตราบเท่าที่ข้อ จำกัด ของคุณถูกต้อง หากคุณต้องการใช้มันในUITableViewCell(เพื่อกำหนดความสูงของแถว) คุณควรเรียกมันว่าเทียบกับมือถือของคุณcontentViewและคว้าความสูง  
มีข้อควรพิจารณาเพิ่มเติมหากคุณมี UILabel หนึ่งรายการขึ้นไปในมุมมองของคุณที่เป็นหลายบรรทัด สำหรับสิ่งเหล่านี้มีความจำเป็นที่preferredMaxLayoutWidthจะต้องตั้งค่าคุณสมบัติอย่างถูกต้องเพื่อให้ฉลากมีความถูกต้องintrinsicContentSizeซึ่งจะใช้ในการsystemLayoutSizeFittingSize'sคำนวณ
แก้ไข: ตามคำขอเพิ่มตัวอย่างของการคำนวณความสูงสำหรับเซลล์มุมมองตาราง
การใช้การคำนวณอัตโนมัติสำหรับการคำนวณความสูงของเซลล์ตารางนั้นไม่ได้มีประสิทธิภาพมากนัก แต่ก็สะดวกสบายโดยเฉพาะถ้าคุณมีเซลล์ที่มีเลย์เอาต์ที่ซับซ้อน  
อย่างที่ฉันได้กล่าวไว้ข้างต้นถ้าคุณใช้ multiline UILabelคุณจำเป็นต้องทำการซิงค์preferredMaxLayoutWidthกับความกว้างของฉลาก ฉันใช้UILabelคลาสย่อยที่กำหนดเองเพื่อทำสิ่งนี้:
@implementation TSLabel
- (void) layoutSubviews
{
    [super layoutSubviews];
    if ( self.numberOfLines == 0 )
    {
        if ( self.preferredMaxLayoutWidth != self.frame.size.width )
        {
            self.preferredMaxLayoutWidth = self.frame.size.width;
            [self setNeedsUpdateConstraints];
        }
    }
}
- (CGSize) intrinsicContentSize
{
    CGSize s = [super intrinsicContentSize];
    if ( self.numberOfLines == 0 )
    {
        // found out that sometimes intrinsicContentSize is 1pt too short!
        s.height += 1;
    }
    return s;
}
@end
นี่คือคลาสย่อย UITableViewController ที่วางแผนไว้ซึ่งแสดงให้เห็นถึง heightForRowAtIndexPath:
#import "TSTableViewController.h"
#import "TSTableViewCell.h"
@implementation TSTableViewController
- (NSString*) cellText
{
    return @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
}
#pragma mark - Table view data source
- (NSInteger) numberOfSectionsInTableView: (UITableView *) tableView
{
    return 1;
}
- (NSInteger) tableView: (UITableView *)tableView numberOfRowsInSection: (NSInteger) section
{
    return 1;
}
- (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath
{
    static TSTableViewCell *sizingCell;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sizingCell = (TSTableViewCell*)[tableView dequeueReusableCellWithIdentifier: @"TSTableViewCell"];
    });
    // configure the cell
    sizingCell.text = self.cellText;
    // force layout
    [sizingCell setNeedsLayout];
    [sizingCell layoutIfNeeded];
    // get the fitting size
    CGSize s = [sizingCell.contentView systemLayoutSizeFittingSize: UILayoutFittingCompressedSize];
    NSLog( @"fittingSize: %@", NSStringFromCGSize( s ));
    return s.height;
}
- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
    TSTableViewCell *cell = (TSTableViewCell*)[tableView dequeueReusableCellWithIdentifier: @"TSTableViewCell" ];
    cell.text = self.cellText;
    return cell;
}
@end
เซลล์ที่กำหนดเองง่าย ๆ :
#import "TSTableViewCell.h"
#import "TSLabel.h"
@implementation TSTableViewCell
{
    IBOutlet TSLabel* _label;
}
- (void) setText: (NSString *) text
{
    _label.text = text;
}
@end
และนี่คือภาพของข้อ จำกัด ที่กำหนดไว้ในกระดานเรื่องราว โปรดทราบว่าไม่มีข้อจำกัดความสูง / ความกว้างบนฉลาก - สิ่งเหล่านี้อนุมานจากฉลากintrinsicContentSize:
