UIView程序化宽度约束

时间:2015-06-09 17:15:33

标签: ios xcode uiview constraints nstableheaderview

我需要添加哪种约束才能确保手机旋转搜索栏并且标签跨越手机的宽度?

下面是我用来启动搜索栏和代码的代码。标签并将它们添加到表头:

self.venueSearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.venueSearchController.searchResultsUpdater = self;
self.venueSearchController.searchBar.delegate = self;
self.venueSearchController.delegate = self;
self.venueSearchController.dimsBackgroundDuringPresentation = NO;
self.venueSearchController.hidesNavigationBarDuringPresentation = YES;
self.venueSearchController.searchBar.frame = CGRectMake(0, 0, self.venueSearchController.view.frame.size.width, 44.0);

// create a UISegmentControl
UILabel *tableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, self.venueSearchController.view.frame.size.width, 30)];
[tableHeaderLabel setTextAlignment:NSTextAlignmentCenter];
tableHeaderLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:18];
tableHeaderLabel.text = @"Text Here";
tableHeaderLabel.backgroundColor = [UIColor redColor];

// create a custom UIView

CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, applicationFrame.size.width, 74)];

[myView addSubview:tableHeaderLabel]; // add header label
[myView addSubview:self.venueSearchController.searchBar]; // add search bar to custom view

self.tableView.tableHeaderView = myView;

1 个答案:

答案 0 :(得分:0)

仅供参考我通过简单地使用autoresizingMask

来解决这个问题
tableHeaderLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

self.venueSearchController.searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;