UITableViewController Table Header Add a UIButton with Autolayout Constraints - ios

I try to add a UIButton to a UITableHeaderFooterView.
It seems that I can only use specific CGRect frame to pin its position and size in code (<-- did a lot of search on StackOverFlow). And the position is very hard to control, always flying around.
UIButton *scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
scanQRCodeButton.frame = CGRectMake(0.0f, 5.0f, 320.0f, 44.0f);
scanQRCodeButton.backgroundColor = [UIColor redColor];
[scanQRCodeButton setTitle:#"Hello" forState:UIControlStateNormal];
[cell addSubview:scanQRCodeButton];
But what I really want is to use "constraints" like centerX, centerY and proportional width to make it adaptive to different screen size. Then the problem comes with the part "constraintWithItems":
Get the headerView from UITableView:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
static NSString *headerReuseIdentifier = #"TableViewSectionHeaderViewIdentifier";
// Reuse the instance that was created in viewDidLoad, or make a new one if not enough.
UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerReuseIdentifier];
if (headerView == nil) {
[tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:#"TableViewSectionHeaderViewIdentifier"];
headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:#"TableViewSectionHeaderViewIdentifier"];
}
headerView.textLabel.text = #"MISSION";
headerView.contentView.backgroundColor = [UIColor blackColor];
Create the UIButton on top of the headerView in the same block as above:
UIButton *goBack = [[UIButton alloc] init];
[goBack setImage:[UIImage imageNamed:#"Back"] forState:UIControlStateNormal];
[goBack addTarget:self
action:#selector(back:)
forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:goBack];
Then set the constraints and return the headerView:
NSLayoutConstraint *constraint = [NSLayoutConstraint
constraintWithItem:headerView.goBack
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:0.5
constant:0];
[headerView addConstraint:constraint];
return headerView;
Problem: when I say "headerView.goBack" in "constraintWithItem", the compiler warns me that "Property goBack not found on object of type UITableHeaderFooterView".
Update 1
From Natarajan's answer, I try to do this now:
headerView.translatesAutoresizingMaskIntoConstraints = false;
goBack.translatesAutoresizingMaskIntoConstraints = false;
NSDictionary *metrics = #{ #"width" : #(CGRectGetWidth(self.view.frame) / 10) };
[headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[goBack]|" options:0 metrics:metrics views:#{#"goBack":goBack}]];
[headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[goBack]|" options:0 metrics:metrics views:#{#"goBack":goBack}]];
Current Problem: The "metrics" part does not work. I want to have proportional width to the headerView or tableView instead of fixed numbers. Should I use "self.view.frame" (also tried self.tableView.frame)?
Update 2
There is also a problem with the headerView now. If I use "headerView.translatesAutoresizingMaskIntoConstraints = false;", I also need to manually set headerView's position. However, with the following code, headerView can still not be set properly. I tried to add it to tableView first and set it the same way as UIButton but it couldn't work the same way.
[headerView addSubview:goBack];
[tableView addSubview:headerView];
headerView.translatesAutoresizingMaskIntoConstraints = false;
goBack.translatesAutoresizingMaskIntoConstraints = false;
[headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[goBack(40.0)]|" options:0 metrics:nil views:#{#"goBack":goBack}]];
[headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[goBack(40.0)]|" options:0 metrics:nil views:#{#"goBack":goBack}]];
[tableView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[headerView]|" options:0 metrics:nil views:#{#"headerView":headerView}]];
[tableView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[headerView]|" options:0 metrics:nil views:#{#"headerView":headerView}]];

I think "goBack" can't accessed as headerView property. Try to find it.
for (UIView *view in headerView.contentView.subviews) {
if ([view isKindOfClass:[UIButton class]]) {
//found button
}
}

Complier warning is correct as UITableViewHeaderFooterView doesn't have the property named "goBack" which you have just added as a subview of UITableViewHeaderFooterView. So Just try to pass the "goBack" button in your constraint code as like below.
To set goBack button fully occupied on HeaderView:
[headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[goBack]|" options:0 metrics:nil views:#{#"goBack":goBack}]];
[headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[goBack]|" options:0 metrics:nil views:#{#"goBack":goBack}]];
To set goBack button for specific width and height:
[headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[goBack(40.0)]" options:0 metrics:nil views:#{#"goBack":goBack}]];
[headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[goBack(30.0)]" options:0 metrics:nil views:#{#"goBack":goBack}]];
Note: Before adding constraint don't forgot to set the headerView.translatesAutoresizingMaskIntoConstraints = false; and goBack.translatesAutoresizingMaskIntoConstraints = false;

Related

How do I set Section-Header.xib constraints in UITableView programmatically using Objective-C

I want to set the width constraints programmatically using Objective-C.
The below code is working fine when I build my app using Xcode 7.2.
[secHeaderView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[_sectionHeaderView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_sectionHeaderView)]];
[secHeaderView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[_sectionHeaderView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_sectionHeaderView)]];
Now when I run my app using Xcode 9.3, then my secHeaderView's are missing. It's due to the constraints which are mentioned above.
If I comment out the above constraints, then I can see my secHeader View, but the width is not properly set. It shows 70% width of UIScreen.
This is how my secHeaderView looks, but the width is not properly set...
Here is my actual code:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
_sectionHeaderView = [[NSBundle mainBundle] loadNibNamed:#"BasicProdInfoSectionHeaderView" owner:self options:nil].firstObject;
SectionHeader *currentHeader = [self.titleStringArray objectAtIndex:section];
[_sectionHeaderView setTitleWithText:currentHeader.titleLabel];
_sectionHeaderView.expandableButton.tag = section;
[_sectionHeaderView.expandableButton addTarget:self action:#selector(expandButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
UIView *secHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,[[UIScreen mainScreen] bounds].size.width , 50)];
_sectionHeaderView.translatesAutoresizingMaskIntoConstraints = NO;
[secHeaderView addSubview:_sectionHeaderView];
[secHeaderView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[_sectionHeaderView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_sectionHeaderView)]];
[secHeaderView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[_sectionHeaderView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_sectionHeaderView)]];
if (section != 3) {
secHeaderView.layer.shadowOffset = CGSizeMake(0, 1);
secHeaderView.layer.shadowRadius = 1;
[secHeaderView.layer setShadowColor:[UIColor blackColor].CGColor];
secHeaderView.layer.shadowOpacity = 0.25;
}
return secHeaderView;
}
Replace this two lines
[secHeaderView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[_sectionHeaderView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_sectionHeaderView)]];
[secHeaderView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[_sectionHeaderView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_sectionHeaderView)]];
With
[secHeaderView
addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[_sectionHeaderView
]|" options:0 metrics:nil views:#{#"_sectionHeaderView
":_sectionHeaderView
}]];
[secHeaderView
addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[_sectionHeaderView
]|" options:0 metrics:nil views:#{#"_sectionHeaderView
":_sectionHeaderView
}]];
Use below constraints I have change below constraints
// center sectionHeaderView horizontally in secHeaderView
[secHeaderView addConstraint:[NSLayoutConstraint constraintWithItem:sectionHeaderView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:secHeaderView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
// center sectionHeaderView vertically in secHeaderView
[secHeaderView addConstraint:[NSLayoutConstraint constraintWithItem:sectionHeaderView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:secHeaderView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
// width constraint
[secHeaderView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:[sectionHeaderView(==0)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(sectionHeaderView)]];
// height constraint
[secHeaderView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[sectionHeaderView(==0)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(sectionHeaderView)]];
I would suggest this. You dont have to set the frame for the section superview. As tableview sets its autolayout automatically.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
_sectionHeaderView = [[NSBundle mainBundle] loadNibNamed:#"BasicProdInfoSectionHeaderView" owner:self options:nil].firstObject;
SectionHeader *currentHeader = [self.titleStringArray objectAtIndex:section];
[_sectionHeaderView setTitleWithText:currentHeader.titleLabel];
_sectionHeaderView.expandableButton.tag = section;
[_sectionHeaderView.expandableButton addTarget:self action:#selector(expandButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
UIView *secHeaderView = [[UIView alloc] init];
_sectionHeaderView.translatesAutoresizingMaskIntoConstraints = NO;
[secHeaderView addSubview:_sectionHeaderView];
[_sectionHeaderView.bottomAnchor constraintEqualToAnchor:secHeaderView.bottomAnchor constant:0]
[_sectionHeaderView.leadingAnchor constraintEqualToAnchor:secHeaderView.leadingAnchor constant:0]
[_sectionHeaderView.trailingAnchor constraintEqualToAnchor:secHeaderView.trailingAnchor constant:0]
if (section != 3) {
secHeaderView.layer.shadowOffset = CGSizeMake(0, 1);
secHeaderView.layer.shadowRadius = 1;
[secHeaderView.layer setShadowColor:[UIColor blackColor].CGColor];
secHeaderView.layer.shadowOpacity = 0.25;
}
[secHeaderView layoutIfNeeded]
return secHeaderView;
}
According to Apple document for tableView:viewForHeaderInSection: method
This method only works correctly when tableView:heightForHeaderInSection: is also implemented.
So I guess that problem is because you didn't implement tableView:heightForHeaderInSection: method in your class.
Another problem is you don't need to give secHeaderView a size inside tableView:viewForHeaderInSection: because
A section's header will always have same width with tableView by default.
The right way to change tableView section's height is using UITableViewDelegate's tableView:heightForHeaderInSection: method.
Actually I created a new project and tried your code. It works as expected.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
_sectionHeaderView = [[NSBundle mainBundle] loadNibNamed:#"BasicProdInfoSectionHeaderView" owner:self options:nil].firstObject;
SectionHeader *currentHeader = [self.titleStringArray objectAtIndex:section];
[_sectionHeaderView setTitleWithText:currentHeader.titleLabel];
_sectionHeaderView.expandableButton.tag = section;
[_sectionHeaderView.expandableButton addTarget:self action:#selector(expandButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
UIView *secHeaderView = [[UIView alloc] initWithFrame:_sectionHeaderView.frame];
_sectionHeaderView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[secHeaderView addSubview:_sectionHeaderView];
if (section != 3) {
secHeaderView.layer.shadowOffset = CGSizeMake(0, 1);
secHeaderView.layer.shadowRadius = 1;
[secHeaderView.layer setShadowColor:[UIColor blackColor].CGColor];
secHeaderView.layer.shadowOpacity = 0.25;
}
return secHeaderView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 50;
}

Align UIButtons horizontally using Auto layout (programmatically)

I'm trying to place 2 UIButtons horizontally but no luck
I'm using auto layout constraints programmatically.
I don't want to put them in a container and align center to that container.
NSDictionary *metrics = #{#"kLeftPadding":#(kLeftPadding),
#"kRightPadding":#(kRightPadding),
#"kMiddlePadding":#(kMiddlePadding),
#"kBottomPadding":#(kBottomPadding)};
NSDictionary *constrainedViews = #{#"titleLabel": self.titleLabel,
#"btnToday" : self.btnToday,
#"btnPickaDay" : self.btnPickaDay,
#"dividerView" : dividerView};
NSArray *contraints = #[#"V:|-[titleLabel]-kBottomPadding-[btnToday(==40)]-[btnPickaDay(==btnToday)]-kBottomPadding-[dividerView]-0-|",
#"H:|-kLeftPadding-[titleLabel]-|",
#"H:|-kLeftPadding-[btnToday]-kMiddlePadding-[btnPickaDay(==btnToday)]-kRightPadding-|",
#"H:|-0-[dividerView]-0-|"];
second button starts from where first ends but in next line, not on same.
I've tried your code but it's getting crashed so as per your question you simply need to write below code.
UILabel *lblText = [UILabel new];
[lblText setText:#"Some Title Text"];
[lblText setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:lblText];
UIButton *btnRed = [UIButton new];
[btnRed setBackgroundColor:[UIColor redColor]];
[btnRed setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:btnRed];
UIButton *btnGreen = [UIButton new];
[btnGreen setBackgroundColor:[UIColor greenColor]];
[btnGreen setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:btnGreen];
UIView *vwLine = [UIView new];
[vwLine setBackgroundColor:[UIColor blueColor]];
[vwLine setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:vwLine];
NSDictionary *dictViews = #{#"red":btnRed,#"green":btnGreen,
#"line":vwLine,#"lbl":lblText};
NSDictionary *dictMetrics = #{#"height":#(40),#"offset":#(-40)};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-20-[red]-0-[green(==red)]-20-|" options:0 metrics:nil views:dictViews]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-20-[line]-20-|" options:0 metrics:nil views:dictViews]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-20-[lbl]-20-|" options:0 metrics:nil views:dictViews]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-(height)-[lbl]-(height)-[red(height)]-(offset)-[green(==red)]-50-[line(2)]" options:0 metrics:dictMetrics views:dictViews]];
result:
Hope it will help you solve your problem. If any more query do let me know.

Not able to set the width of button with autolayout

I am new in auto layout and used auto layout in scrollview i have some simple setup in UIScrollView but i don't achieve it.
First think i need to discuss is that i used auto layout in coding not in storyboard.
I have 20 button of same width and height and want to setup in UIScrollView.
But surprise my button width set as per contain insert into it. I try lots R&D but not able to find the solution
My code is following
UIScrollView *sview = [[UIScrollView alloc] init];
[sview setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.7]];
[sview setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:sview];
NSDictionary *dict = NSDictionaryOfVariableBindings(sview);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|[sview]|" options:0 metrics:nil views:dict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-60-[sview]-|" options:0 metrics:nil views:dict]];
setup scrollview whose width is same as view
NSMutableDictionary *dictValues = [NSMutableDictionary dictionaryWithDictionary:#{#"xpos":#10,#"ypos":#10,#"height":#30}];
UIButton *previousBtn;
for (int i=0; i<20; i++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:[NSString stringWithFormat:#"%d",i+1] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor redColor]];
[btn setTranslatesAutoresizingMaskIntoConstraints:false];
[sview addSubview:btn];
if (previousBtn == nil)
{
[sview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[btn]-|" options:0 metrics:dictValues views:#{#"btn":btn}]];
[sview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-(ypos)-[btn(height)]" options:0 metrics:dictValues views:#{#"btn":btn}]];
}
else
{
[sview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[btn]-|" options:0 metrics:dictValues views:#{#"btn":btn}]];//Look here i set button width same as a scrollview width but not able to do that
[sview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[previousBtn]-10-[btn(height)]" options:0 metrics:dictValues views:#{#"btn":btn,#"previousBtn":previousBtn}]];
}
previousBtn = btn;
}
NSDictionary *dictBtn = NSDictionaryOfVariableBindings(previousBtn);
[sview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[previousBtn]-|" options:0 metrics:dictValues views:dictBtn]];
[sview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[previousBtn(height)]-|" options:0 metrics:dictValues views:dictBtn]];
Here are screenshot of this code
In portrait
In Landscape
Look in screen shot button is same of the text size not like scroll view and one more thing i am used pure autolayout

Why are items outside the view?

I had two buttons in a UIView, then I add the view to main view, but I eventually got something like this:
As you can see these two buttons went outside the red view.
I wanted a little bit of margin on top, so I use constraintsWithVisualFormat:#"V:|-100-[buttonGroup]-10-|", I'm not sure if that matters.
Here's the original code:
- (UIButton*) getButtonWithTitle: (NSString*) title
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.layer.borderColor = [UIColor blackColor].CGColor;
button.layer.borderWidth = 0.5f;
button.layer.cornerRadius = 2.0f;
[button setTranslatesAutoresizingMaskIntoConstraints:NO];
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
return button;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
UIButton *loginBtn = [self getButtonWithTitle:#"Login"];
UIButton *registerBtn = [self getButtonWithTitle:#"Register"];
UIView *buttonGroup = [[UIView alloc] init];
[buttonGroup setTranslatesAutoresizingMaskIntoConstraints:NO];
[buttonGroup addSubview: loginBtn];
[buttonGroup addSubview: registerBtn];
[self.view addSubview: buttonGroup];
[buttonGroup setBackgroundColor:[UIColor redColor]];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(loginBtn, registerBtn, buttonGroup);
[buttonGroup addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[loginBtn]-20-[registerBtn(==loginBtn)]-|"
options:0
metrics:nil
views:viewsDictionary
]];
NSArray *verticalConstraints = [NSLayoutConstraint
constraintsWithVisualFormat:#"V:|-100-[buttonGroup]-10-|"
options:0
metrics:nil
views:viewsDictionary];
NSArray *horizontalConstraints = [NSLayoutConstraint
constraintsWithVisualFormat:#"H:|-[buttonGroup]-|"
options:0
metrics:nil
views:viewsDictionary];
[self.view addConstraints: horizontalConstraints];
[self.view addConstraints: verticalConstraints];
}
EDIT
The constraints I added:
[buttonGroup addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-[loginBtn]-|"
options:0
metrics:nil
views:viewsDictionary
]];
[buttonGroup addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-[registerBtn]-|"
options:0
metrics:nil
views:viewsDictionary
]];
It looks like I did extra work ...
You never created any vertical constraints between the buttons and buttonGroup; add those, and you should be good. Also, you shouldn't set translatesAutoresizingMaskIntoConstraints to NO for the controller's self.view (only for subviews that you add to it).
In the VFL , the | means the superview, but your two buttons(loginBtn, registerBtn) are the subviews of the buttonGroup. So you should define the V relation about the loginBtn, registerBtn with the buttonGroup. As rdelmar said, the self.view should set the translatesAutoresizingMaskIntoConstraints to No. This can close the autosizingmask function. I think the best way to do this things in xib or storyboard.

How to add constraints programatically to place a label at the left bottom of the screen?

I want to place a label at the bottom left of the screen: So I added the following code:
UILabel *versionLabel = [UILabel new];
versionLabel.text = #"some text";
versionLabel.textColor = [UIColor whiteColor];
versionLabel.font = [UIFont systemFontOfSize:10.0];
[self.view addSubview:versionLabel];
[versionLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
NSDictionary *views = NSDictionaryOfVariableBindings(versionLabel);
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"H:|-3-[versionLabel]"
options:0
metrics:nil
views:views]];
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"V:[versionLabel]-3-|"
options:0
metrics:nil
views:views]];
My issue is that the label ends up at the top left always. No way to move it at the bottom when I add constraints.
If you wrap the UITableViewController in a UIViewController, you can layout the label and table view as you see fit.
#import "WrappingViewController.h"
#import "YourCustomTableViewController.h"
#implementation WrappingViewController {
}
- (void)viewDidLoad {
// I instantiate this manually, but you could use an outlet from interface builder
UITableViewController *tableViewController = [YourCustomTableViewController new];
[self addChildViewController:tableViewController];
UIView *tableView = tableViewController.view;
UILabel *label1 = [UILabel new];
label1.text = #"Some text";
label1.font = [UIFont systemFontOfSize:10.0];
label1.translatesAutoresizingMaskIntoConstraints = NO;
tableView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:tableView];
[self.view addSubview:label1];
NSDictionary *views = NSDictionaryOfVariableBindings(tableView, label1);
[self.view addConstraints:
[NSLayoutConstraint
constraintsWithVisualFormat:#"H:|[tableView]|" // make the tableview take the whole width
options:0
metrics:nil
views:views]
];
[self.view addConstraints:
[NSLayoutConstraint
constraintsWithVisualFormat:#"H:|-3-[label1]" // attach the label 3 pixels from the left
options:0
metrics:nil
views:views]
];
[self.view addConstraints:
[NSLayoutConstraint
constraintsWithVisualFormat:#"V:|[tableView]-[label1]-3-|" // attach tableview to top, then space, then label, 3px, then bottom
options:0
metrics:nil
views:views]
];
}
#end

Resources