iOS 7.1 UITableView layoutSubviews问题

时间:2015-01-07 15:10:26

标签: ios objective-c iphone uitableview

我正在开发一个必须同时支持iOS 8和iOS 7.1的项目。现在我遇到的问题只出现在iOS 7.1上,但在iOS 8上运行正常。我有一个ViewController,它包含tableView和tableHeaderView中的自定义视图。我将发布如下代码。所有约束都以编程方式添加。

//查看控制器。

-(void)viewDidLoad
{
  [super viewDidLoad];

  self.commentsArray = [NSMutableArray new];

  [self.commentsArray addObject:@"TEST"];
  [self.commentsArray addObject:@"TEST"];
  [self.commentsArray addObject:@"TEST"];
  [self.commentsArray addObject:@"TEST"];
  [self.commentsArray addObject:@"TEST"];
  [self.commentsArray addObject:@"TEST"];

  [self.view addSubview:self.masterTableView];

  self.masterTableView.tableHeaderView = self.detailsView;

  [self.view setNeedsUpdateConstraints];


}

- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  [self.view layoutIfNeeded];
}


//Table view getter
- (UITableView *)masterTableView
{
  if(!_masterTableView)
  {
    _masterTableView = [UITableView new];
    _masterTableView.translatesAutoresizingMaskIntoConstraints = NO;

    _masterTableView.backgroundColor                = [UIColor greyColor];

    _masterTableView.delegate                       = self;
    _masterTableView.dataSource                     = self;

    _masterTableView.separatorStyle                 = UITableViewCellSeparatorStyleNone;
    _masterTableView.showsVerticalScrollIndicator   = NO;
    _masterTableView.separatorInset                 = UIEdgeInsetsZero;

  }

  return _masterTableView;
}

-(void)updateViewConstraints
{
 NSDictionary *views = @{
      @"table"        : self.masterTableView,
      @"details"      : self.detailsView,
 };


  NSDictionary *metrics = @{
                            @"width"  : @([UIScreen mainScreen].applicationFrame.size.width)
                            };
    //Table view constraints
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[table]-0-|" options:0 metrics:0 views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[table]-0-|" options:0 metrics:0 views:views]];

  //Details View
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[details(width)]-0-|" options:0 metrics:metrics views:views]];
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[details]-0-|" options:0 metrics:metrics views:views]];

}


//Details View Getter
- (DetailsView *)detailsView
{
  if(!_detailsView)
  {

   _detailsView = [[DetailsView alloc]initWithFrame:CGRectMake(0, 0, 0, 100)];
    _detailsView.backgroundColor = [UIColor orangeColor];

  return _detailsView;
}

现在详细信息视图包含一些基本的子视图,这些子视图都来自UIView,详细信息视图本身来自更一般的超类。我将发布如下代码。

//Parent View 
@interface ParentView (): UIView

- (instancetype)init
{
  self = [super init];

  if (self)
  {
    [self setupViews];
  }

  return self;
}


- (void)setupViews
{
  [self addSubview:self.publishedTimeView];
}

- (void)updateConstraints
{
  NSDictionary *views = @{
                          @"time"         : self.publishedTimeView,
                          };


  // Header with published video time
  [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[time]-10-|" options:0 metrics:0 views:views]];
  [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[time(11)]" options:0 metrics:0 views:views]];

  [super updateConstraints];
}

//Getter for the timePublished view added to the detail view. Will not post all its related code for //the sake of brevity.
- (TimePublishedDetailView *)publishedTimeView
{
  if(!_publishedTimeView)
  {
    _publishedTimeView = [TimePublishedDetailView new];
    _publishedTimeView.translatesAutoresizingMaskIntoConstraints = NO;
  }

  return _publishedTimeView;
}

//Child View (or the detailsView) of the view controller.

@interface DetailsView : ParentView


@implementation RecordingDetailsView

- (void)updateConstraints
{
  NSDictionary *views = @{
                          @"time"         : self.publishedTimeView,
                          };

   //Vertical alignment of all views

  [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[time]" options:0 metrics:nil views:views]];

  [super updateConstraints];

}


- (void)setModel:(DetailViewModel *)model
{
  self.publishedTimeView.date = model.dateTime;

  [self setNeedsUpdateConstraints];
  [self layoutSubviews];
}

现在在iOS 8上看起来像这样: enter image description here

但是在iOS 7.1上,这会出现此错误消息:

“异常:执行-layoutSubviews后仍然需要自动布局.UITableView的-layoutSubviews实现需要调用super”

我已经使用Google搜索并在我的布局调用中使用代码来查看是否可以解决问题,但到目前为止都没有成功。如果有人可以发布一些关于如何解决这个问题的技巧或建议,我会非常感激。

1 个答案:

答案 0 :(得分:2)

我知道这个问题已经过时了,但我最近遇到了类似的问题,经过大量谷歌搜索,尝试失败后,我在this answer的帮助下进行了一些改动。

只需将此类别添加到您的项目中,只需拨打[UITableView fixLayoutSubviewsMethod];一次(我建议在AppDelegate内)。

#import <objc/runtime.h>
#import <objc/message.h>

@implementation UITableView (FixUITableViewAutolayoutIHope)

+ (void)fixLayoutSubviewsMethod
{
    Method existing = class_getInstanceMethod(self, @selector(layoutSubviews));
    Method new = class_getInstanceMethod(self, @selector(_autolayout_replacementLayoutSubviews));

    method_exchangeImplementations(existing, new);
}

- (void)_autolayout_replacementLayoutSubviews
{
    [super layoutSubviews];
    [self _autolayout_replacementLayoutSubviews]; // not recursive due to method swizzling
    [super layoutSubviews];
}

@end