I've been struggling with this issue the whole day and would really require some help. What I'm trying to do is create a view whose frame adjusts based on the subviews with it. Based on certain business rules I have to add and remove subviews. Here is some sample code:
-(void)createLinkedAccountDetailsHeaderView {
CGRect frame = CGRectMake(0, 0, 600, 220);
CDPAccountDetailsView *accountDetailsView = [[CDPAccountDetailsView alloc ] initWithFrame:frame withViewModel:self.viewModel isInEditState:self.isInEditState];
self.tableView.tableHeaderView = accountDetailsView;
}
- (instancetype)initWithFrame:(CGRect)frame withViewModel:(CDPDebitOrderDetailsReviewViewModel *)viewModel isInEditState:(BOOL) isInEditState {
self = [super initWithFrame:frame];
if (self) {
self.viewModel = viewModel;
self.isInEditState = isInEditState;
[self initialiseView];
[self configureWithAccountsView];
}
return self;
}
(void)initialiseView {
NSString *nibName = NSStringFromClass([self class]);
UINib *nib = [UINib nibWithNibName:nibName bundle:[SBResourcesBundle bundleWithName:[CDPResourceBundle bundleName]]];
[nib instantiateWithOwner:self options:nil];
[SBViewUtility addSubview:self.mainView withConstraintsToParentView:self];
[self layoutIfNeeded];
self.circleView.layer.cornerRadius = self.circleView.bounds.size.width /2;
//A lot of subview initialisation is done
[self layoutIfNeeded];
}
How can I get the UIView to wrap around its subviews and change its frame based on its subviews? As it currently always uses the fixed frame regardless of its subviews.