I've read the official documentation from Apple on auto layout and UIScrollView:
RN-iOSSDK-6_0
I'm trying the "Pure Auto Layout approach".
I'm adding a UILabel and a UITextField. I want both to expand to the width of the interface:
RootViewController.h
#interface rootViewController : UIViewController
#property (nonatomic, strong) UIScrollView *scrollView;
#property (nonatomic, strong) UIView *contentView;
#property (nonatomic, strong) UILabel *bigLetters;
#property (nonatomic, strong) UITextField *aTextField;
#end
viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
self.view.backgroundColor = [UIColor lightGrayColor];
_scrollView = [[UIScrollView alloc] init];
[_scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
_contentView = [[UIView alloc] init];
[_contentView setTranslatesAutoresizingMaskIntoConstraints:NO];
_bigLetters = [[UILabel alloc] init];
[_bigLetters setTranslatesAutoresizingMaskIntoConstraints:NO];
_bigLetters.font = [UIFont systemFontOfSize:30];
_bigLetters.textColor = [UIColor blackColor];
_bigLetters.backgroundColor = [UIColor clearColor];
_bigLetters.text = #"Why so small?";
_bigLetters.textAlignment = NSTextAlignmentCenter;
_aTextField = [[UITextField alloc] init];
_aTextField.backgroundColor = [UIColor whiteColor];
_aTextField.placeholder = #"A Text Box";
[_aTextField setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:_scrollView];
[self.scrollView addSubview:_contentView];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[_scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[_scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)]];
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[_contentView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_contentView)]];
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[_contentView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_contentView)]];
[self.contentView addSubview:_bigLetters];
[self.contentView addSubview:_aTextField];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[_bigLetters]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_bigLetters)]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[_aTextField]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_aTextField)]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-[_bigLetters]-[_aTextField]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_bigLetters, _aTextField)]];
// Do any additional setup after loading the view.
}
I get both elements squished against the top left corner like this:
If I switch _scrollView to a UIView, things look fine. What am I doing wrong?
Thanks!
UIScrollViews are a little bit complicated.
Firs of all:
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
You should not be doing that. Your root view does not need to work with autolayout. Your subviews need it.
Also, the contentSize of the scrollview is defined by what you put inside of them. If you want to have a scrollView screen-sized fix one of it's subviews to screen width (or eight).
What you are seeing there, is your scrollview wrapping up it's content, and the width is probably the label intrinsic content size.
That's why it works with a View.
Hope it helps!!
Related
I am adding a subview to a UICollectionViewCell. I am programmatically adding constraints to make it fill the cell, however the width does not fill the cell.
When I view it in the view debugger, it says the position is ambiguous. How can this be since I am specifying all 4 sides are pinned to the superview?
This is what the views look like in the debugger. The inner, white view should fit the parents width (blue border):
Inspecting the constraints on the parent view shows this with the "position ambiguous" warning:
The code I am using is as follows:
[self.contentView addSubview:calloutView];
calloutView.translatesAutoresizingMaskIntoConstraints = NO;
self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-0-[calloutView]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(calloutView) ]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-0-[calloutView]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(calloutView) ]];
The problem seems to be related to the use of contentView, you must use the cell itself to achieve what you want, this is the code and works, check the picture below
#import "CollectionViewCell.h"
#interface CollectionViewCell()
#property UIView * testView;
#end
#implementation CollectionViewCell
#synthesize testView;
-(void)awakeFromNib
{
[super awakeFromNib];
testView = [[UIView alloc]initWithFrame:CGRectZero];
testView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:testView];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-2-[testView]-2-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(testView) ]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-2-[testView]-2-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(testView) ]];
self.testView.layer.borderWidth = 2;
self.testView.layer.borderColor = [UIColor blueColor].CGColor;
}
#end
I hope this helps you, best regards
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.
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
I am here to get help for a simple problem.My app runs in both 3.5" and 4" screens but the view is not centred in both the sizes(check images for the view).I am using auto layout and also tried to reset suggested constraints.When i bring the label to centre in one view it is not positioned correctly in the other view.Please help i am stuck
You need to add 4 constraints to the view which needs to be centered.
Pin:
1. Width.
2. Height.
Align:
3. Horizontal center in container.
4. Vertical center in container.
Wrap all your subviews except the red grandient background inside a container UIView:
#property (nonatomic, strong) UIView *container;
[self.container addSubview:greyBox];
[self.container addSubview:getStarted];
...
[self.view addSubview:self.redBG];
[self.view addSubview:self.container];
Update your constraints to be relative to your container view instead of self.view.
Then center your container view to your self.view:
// ---------------------------------------------------------------
// centers the container view to your view controller's vivew
// ---------------------------------------------------------------
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.container attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.container attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
That way, your subview always appear to be centered.
I think the problem you're having at the moment is that you 'offset' each subview's y position relative to the self.view's top edge. You might get it right on one screen, but then when your screen gets longer, it appears wrong.
Using the above view container method, lets you dynamically add as much subviews as you like and it is still centered, assuming your subviews are connected to the container view's top and bottom edges like a tent.
Floating subviews inside a container view will cause the container to have 0 height and not work.
Update
Not sure how you've setup your IB constraints, you might want to show screenshots of them.
Perhaps you have not setup your constraints properly in IB. Maybe you can improve your constraint settings similar to the ones I've provided below to see if it fixes it.
This is a demo of the method I mentioned above:
4 inch iPhone screen:
3.5 inch iPhone screen:
Notice how both screenshot shows the white box being centered in the view.
This is the code I used:
Header File
// HEADER FILE
#import <UIKit/UIKit.h>
#interface MainViewController : UIViewController
#property (nonatomic, strong) UIView *gradient;
#property (nonatomic, strong) UIView *container;
#property (nonatomic, strong) UIView *whiteBox;
#property (nonatomic, strong) UILabel *label1;
#property (nonatomic, strong) UIButton *getStarted;
#property (nonatomic, strong) UILabel *label2;
#end
Implementation File
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self initView];
[self initConstraints];
}
-(void)initView
{
self.gradient = [[UIView alloc] init];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.view.frame;
UIColor *redColor = [UIColor colorWithRed:157.0/255.0 green:37.0/255.0 blue:29.0/255.0 alpha:1.0];
UIColor *pinkColor = [UIColor colorWithRed:216.0/255.0 green:101.0/255.0 blue:100.0/255.0 alpha:1.0];
gradientLayer.startPoint = CGPointMake(0.5, 0.0);
gradientLayer.endPoint = CGPointMake(0.5, 1.0);
gradientLayer.colors = #[(id)redColor.CGColor,
(id)pinkColor.CGColor,
(id)redColor.CGColor];
[self.gradient.layer insertSublayer:gradientLayer atIndex:0];
// container view
self.container = [[UIView alloc] init];
// white box
self.whiteBox = [[UIView alloc] init];
self.whiteBox.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0];
self.whiteBox.layer.shadowColor = [UIColor blackColor].CGColor;
self.whiteBox.layer.shadowOffset = CGSizeMake(0.0, 3.0);
self.whiteBox.layer.shadowOpacity = 0.5;
self.whiteBox.layer.shadowRadius = 3.0;
self.label1 = [[UILabel alloc] init];
self.label1.text = #"Label 1 Text Goes Here";
self.label1.textAlignment = NSTextAlignmentCenter;
self.label1.textColor = redColor;
self.getStarted = [[UIButton alloc] init];
[self.getStarted setTitle:#"Get Started" forState:UIControlStateNormal];
[self.getStarted setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.getStarted.backgroundColor = redColor;
self.label2 = [[UILabel alloc] init];
self.label2.text = #"Version 1.3 (log enabled)";
self.label2.textAlignment = NSTextAlignmentCenter;
self.label2.textColor = [UIColor darkGrayColor];
self.label2.font = [UIFont fontWithName:#"Arial" size:14];
[self.whiteBox addSubview:self.label1];
[self.whiteBox addSubview:self.getStarted];
[self.whiteBox addSubview:self.label2];
[self.container addSubview:self.whiteBox];
[self.view addSubview:self.gradient];
[self.view addSubview:self.container];
}
-(void)initConstraints
{
self.gradient.translatesAutoresizingMaskIntoConstraints = NO;
self.container.translatesAutoresizingMaskIntoConstraints = NO;
self.whiteBox.translatesAutoresizingMaskIntoConstraints = NO;
self.label1.translatesAutoresizingMaskIntoConstraints = NO;
self.getStarted.translatesAutoresizingMaskIntoConstraints = NO;
self.label2.translatesAutoresizingMaskIntoConstraints = NO;
id views = #{
#"gradient": self.gradient,
#"container": self.container,
#"whiteBox": self.whiteBox,
#"label1": self.label1,
#"getStarted": self.getStarted,
#"label2": self.label2
};
// gradient constraint
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[gradient]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[gradient]|" options:0 metrics:nil views:views]];
// container constraitns
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.container
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.container
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
// white box constraints
[self.container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-10-[whiteBox(280)]-10-|" options:0 metrics:nil views:views]];
[self.container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-10-[whiteBox]-10-|" options:0 metrics:nil views:views]];
// white box subview constraints
[self.whiteBox addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-10-[label1]-10-|" options:0 metrics:nil views:views]];
[self.whiteBox addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-10-[getStarted]-10-|" options:0 metrics:nil views:views]];
[self.whiteBox addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-10-[label2]-10-|" options:0 metrics:nil views:views]];
[self.whiteBox addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-10-[label1]-20-[getStarted(40)]-15-[label2]-5-|" options:0 metrics:nil views:views]];
}
I'd like to position a UITextField inside a UIViewController view using auto layout (dynamic resizing and a 5 pt margin between the textfield and its superview). I create the view programmatically using the loadView method.
Header of UIViewController
#property (weak, nonatomic) UITextField *textField;
Implementation
-(void)loadView{
//Frame
UIView *frame = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
[self setView:frame];
[self.view setBackgroundColor:[UIColor redColor]];
//TextField
[self.textField setDelegate:self];
[self.textField setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.textField setBackgroundColor:[UIColor blueColor]];
[frame addSubview:self.textField];
[frame addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-(5#50)-[textField(==10#10)]-(5#50)-|"
options:0
metrics:nil
views:#{#"textField" : self.textField}]];
[frame addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: #"V:|-(5#50)-[textField(==10#10)]-(5#50)-|"
options:0
metrics:nil
views:#{#"textField" : self.textField}]];
// //Test with UIView instead
// UIView *sub = [[UIView alloc]init];
// [sub setTranslatesAutoresizingMaskIntoConstraints:NO];
// [sub setBackgroundColor:[UIColor greenColor]];
// [frame addSubview:sub];
//
// [frame addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-(5#50)-[sub(==10#10)]-(5#50)-|"
// options:0
// metrics:nil
// views:#{#"sub" : sub}]];
//
// [frame addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: #"V:|-(5#50)-[sub(==10#10)]-(5#50)-|"
// options:0
// metrics:nil
// views:#{#"sub" : sub}]];
Running this throws an exception. Curiously using a UIView ("sub") as subview instead (the part commented out) does work as expected.
Ouput:
Shouldn't the textfield behave just like this UIView? How come the textField causes an exception?
New finding: The app crashes only when I use addConstraints on the self.textField. If I remove those two calls I spare the crash, however the textfield doesn't show up either.
NSLayoutConstraint is only part of IOS 6. if you try to work this code other than IOS6
( such as IOS 5, IOS 4.3) you are highly likely get this runtime error.