Centre the button in view use constraints programmatically in iOS 8 - ios

I have a subview and added 1 button. I want to add this button in the center of that view. I use Autolayout so i need to set the constraints for this button programmatically.
I tried this code but the play button is not in the center.
[self.moviePlayer.view addSubview:self.playbtn];
self.playbtn.translatesAutoresizingMaskIntoConstraints = NO;
[self.moviePlayer.view addConstraint:[NSLayoutConstraint constraintWithItem:self.playbtn attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual
toItem:self.moviePlayer.view attribute:NSLayoutAttributeCenterX multiplier:0.667 constant:0]];
Please help me to correct it. Thanks in advance.

You can use the NSLayoutAttributeCenterX and NSLayoutAttributeCenterY attributes to center your play button like this:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initViews];
[self initConstraints];
}
-(void)initViews
{
self.picture = [[UIImageView alloc] init];
self.picture.image = [UIImage imageNamed:#"bg"];
self.playButton = [[UIButton alloc] init];
[self.playButton setImage:[UIImage imageNamed:#"playButton"] forState:UIControlStateNormal];
[self.view addSubview:self.picture];
[self.view addSubview:self.playButton];
}
-(void)initConstraints
{
self.picture.translatesAutoresizingMaskIntoConstraints = NO;
self.playButton.translatesAutoresizingMaskIntoConstraints = NO;
id views = #{
#"picture": self.picture,
#"playButton": self.playButton
};
// picture constraints
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[picture]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[picture]|" options:0 metrics:nil views:views]];
// play button constraints
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.playButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.playButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
}
You get something like this:

You can use this code to center any item (es. self.btn) in its superview:
[self.btn.superview addConstraint:[NSLayoutConstraint
constraintWithItem:self.btn.superview
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.btn
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[self.btn.superview addConstraint:[NSLayoutConstraint
constraintWithItem:self.btn.superview
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.btn
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
I also wrote an article about aligning any UIView objects without using Storyboards on my blog.

Related

Horizontal stacking in scrollview using autolayout

What's the best way to make sure that child views in a UIScrollView are stacked horizontally, one after the other, using autolayout? The number of child views will vary, so I can't use IB, but have to do it programatically. I understand how layout constraints work, but what's the best way to solve it? Loop all child views and update the constraints for each view everytime the number of child views are updated? If so, is there an easy solution to refer to the previous sibling in a layout constraint, or do I have have to keep a reference to the previous sibling?
I wrote the following method for an app. Might not be the prettiest but it gets the job done. It takes the views as an array which solves the problem of having a reference to the previous view. It also uses transparent spacer views to space them evenly but you can easily take those out.
+(void)spaceViews:(NSArray*)views evenlyInContainer:(UIView*)container {
UIView* lastSpacer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
lastSpacer.translatesAutoresizingMaskIntoConstraints = NO;
[lastSpacer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[lastSpacer(10)]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(lastSpacer)]];
[container addSubview:lastSpacer];
[container addConstraint:[NSLayoutConstraint constraintWithItem:container attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:lastSpacer attribute:NSLayoutAttributeLeading multiplier:1.f constant:0]];
for (int i = 0; i < [views count]; i++) {
[container addSubview:views[i]];
[container addConstraint:[NSLayoutConstraint constraintWithItem:lastSpacer attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:views[i] attribute:NSLayoutAttributeLeading multiplier:1.f constant:0]];
UIView* spacerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
[spacerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[spacerView(10)]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(spacerView)]];
spacerView.translatesAutoresizingMaskIntoConstraints = NO;
[spacerView setBackgroundColor:[UIColor blueColor]];
[container addSubview:spacerView];
[container addConstraint:[NSLayoutConstraint constraintWithItem:spacerView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:views[i] attribute:NSLayoutAttributeTrailing multiplier:1.f constant:0]];
[container addConstraint:[NSLayoutConstraint constraintWithItem:spacerView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:container attribute:NSLayoutAttributeCenterY multiplier:1.f constant:0]];
[container addConstraint:[NSLayoutConstraint constraintWithItem:views[i] attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:container attribute:NSLayoutAttributeCenterY multiplier:1.f constant:0]];
if (lastSpacer) {
[container addConstraint:[NSLayoutConstraint constraintWithItem:spacerView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:lastSpacer attribute:NSLayoutAttributeWidth multiplier:1.f constant:0]];
}
lastSpacer = spacerView;
}
[container addConstraint:[NSLayoutConstraint constraintWithItem:lastSpacer attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:container attribute:NSLayoutAttributeTrailing multiplier:1.f constant:0]];
}

How to horizontally center UITextView content in iOS, but not using Align Center?

I want to achieve something very specific. It is hard to describe in words so here are some mockups..
Current stage: I have a UITextView which has autolayout constraints set to be as wide as the parent container.
What I want to achieve: I would like to horizontally center the contents in the UITextView, so that they appear in center but are still left aligned
Most solutions rely on TextAlignCenter option to center the text horizontally, however this is not something that I want as it will appear as such instead:
Any way to achieve this?
I've tried the below:
textview.sizeToFit() seems to adjust the height but not the width
textview.contentOffset does not seem to work
Removing the constraints in autolayout and use Editor > Fix all in scope seems to work on UIButtons but not UITextView..
The reason I want to do this is although it is not noticable on iPhones, on iPads if the text are short they appear all the way to the left, which is very far from my other interactable buttons in my app.
It looks like you are probably using Swift, but that's fine, here's the code for Objective-C, just convert it.
UITextView * _descriptionText = [UITextView new];
[_descriptionText setDelegate:self];
[_descriptionText setBackgroundColor:[UIColor whiteColor];
[_descriptionText setTranslatesAutoresizingMaskIntoConstraints:false];
[_descriptionText setTextColor:[UIColor lightGrayColor]];
[_descriptionText setTextAlignment:NSTextAlignmentLeft];
then, add this textview to a UIView
[sss addSubview:_descriptionText];
Here's the code for the UIView:
UIView * sss = [UIView new];
[sss setTranslatesAutoresizingMaskIntoConstraints:FALSE];
[self addSubview:sss];
Add constraints to the UIView to center the UITextView and to constrain it's bounds by a certain Width on the left and right sides:
[sss addConstraint:[NSLayoutConstraint constraintWithItem:descriptionText attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:sss attribute:NSLayoutAttributeHeight multiplier:1.0f constant:SOME_HEIGHT_NUMBER_YOU_CHOOSE]];
[sss addConstraint:[NSLayoutConstraint constraintWithItem:descriptionText attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:sss attribute:NSLayoutAttributeHeight multiplier:1.0f constant:SOME_RIGHT_MARGIN_NUMBER_YOU_CHOOSE]];
[sss addConstraint:[NSLayoutConstraint constraintWithItem:descriptionText attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:sss attribute:NSLayoutAttributeLeft multiplier:1.0f constant:SOME_LEFT_MARGIN_NUMBER_YOU_CHOOSE]];
or choose a width for the UITextView like this with a CenterX constraint:
[sss addConstraint:[NSLayoutConstraint constraintWithItem:descriptionText attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:SOME_WIDTH_NUMBER_YOU_CHOOSE]];
[sss addConstraint:[NSLayoutConstraint constraintWithItem:descriptionText attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:sss attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0]];
And finally, add a CenterY constraint:
[sss addConstraint:[NSLayoutConstraint constraintWithItem:descriptionText attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:sss attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0]];
Lastly, constrain the UIView "sss" to it's super view "self" or whatever it's superview is:
NSDictionary * views = NSDictionaryOfVariableBindings(sss);
NSDictionary * metrics = #{#"bh" : #30, #"bsh" : #40, ... etc, etc,};
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[sss]|" options:0 metrics:metrics views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-bh-[sss]" options:0 metrics:metrics views:views]];
and then, profit!
Formatted code with subclass UIView
ExampleView.m
#implementation ExampleView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
UIView * sss = [UIView new];
[sss setTranslatesAutoresizingMaskIntoConstraints:false];
[self addSubview:sss];
UITextView * _descriptionText = [UITextView new];
[_descriptionText setBackgroundColor:[UIColor whiteColor]];
[_descriptionText setTranslatesAutoresizingMaskIntoConstraints:false];
[_descriptionText setTextColor:[UIColor lightGrayColor]];
[_descriptionText setTextAlignment:NSTextAlignmentLeft];
[sss addSubview:_descriptionText];
//do these three constraints
[sss addConstraint:[NSLayoutConstraint constraintWithItem:_descriptionText attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:sss attribute:NSLayoutAttributeHeight multiplier:1.0f constant:SOME_HEIGHT_NUMBER_YOU_CHOOSE]];
[sss addConstraint:[NSLayoutConstraint constraintWithItem:_descriptionText attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:sss attribute:NSLayoutAttributeRight multiplier:1.0f constant:SOME_RIGHT_MARGIN_NUMBER_YOU_CHOOSE]];
[sss addConstraint:[NSLayoutConstraint constraintWithItem:_descriptionText attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:sss attribute:NSLayoutAttributeLeft multiplier:1.0f constant:SOME_LEFT_MARGIN_NUMBER_YOU_CHOOSE]];
//or do these three constraints, but don't do all 6 constraints between these three and the three above
[sss addConstraint:[NSLayoutConstraint constraintWithItem:_descriptionText attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:SOME_WIDTH_NUMBER_YOU_CHOOSE]];
[sss addConstraint:[NSLayoutConstraint constraintWithItem:_descriptionText attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:sss attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0]];
[sss addConstraint:[NSLayoutConstraint constraintWithItem:_descriptionText attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:sss attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0]];
NSDictionary * views = NSDictionaryOfVariableBindings(sss);
NSDictionary * metrics = #{#"bh" : #30, #"bsh" : #40};
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[sss]|" options:0 metrics:metrics views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-bh-[sss]" options:0 metrics:metrics views:views]];
// this: V:|-bh-[sss] SHOULD WORK, BUT IT MIGHT REQUIRE YOU TO DO THIS: V:|-bh-[sss(HEIGHT_OF_THE_TEXT_VIEW_THAT_YOU_CHOOSE)]
}
return self;
}
#end
ExampleView.h
#import <UIKit/UIKit.h>
#interface NSHMessagesView : UIView
#end
I think you can subclass UITextView or set contentInset. beacause it's subclass scrollView, so i guess set contentInset can achevie the goal.

Autolayout in code ? height issue?

I have Two View say view1 and view2.
- view2 should be at 2 pixel distance from view1 (vertically) and
- Height of view2 should be such that bottom of view2 must be same as bottom of viewcontroller.
How to specify the constraint for this in code ?
Since I don't know what you already have in your view hierarchy I've set up a simple example. Look into this bit of code. It creates two new UIViews and position them using constraints.
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view1 = [UIView new];
UIView *view2 = [UIView new];
view1.backgroundColor = [UIColor redColor];
view2.backgroundColor = [UIColor blueColor];
[self.view addSubview:view1];
[self.view addSubview:view2];
[view1 setTranslatesAutoresizingMaskIntoConstraints:NO];
[view2 setTranslatesAutoresizingMaskIntoConstraints:NO];
NSDictionary *views = NSDictionaryOfVariableBindings(view1, view2);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-(100)-[view1(200)]-(2)-[view2]|" options:0 metrics:0 views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|[view1]|" options:0 metrics:0 views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|[view2]|" options:0 metrics:0 views:views]];
}
Here's the result:
Code is not that hard to understand. + constraintsWithVisualFormat: method creates an array of NSLayoutConstraints that you add to the superview of your custom views. Look here for more on visual format language. It's very effective at simple layout and saves a bunch of time compared to creating constraints one by one.
If you reeeaaally want to create constraints using constraintWithItem: method, here's your code:
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:100]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeBottom multiplier:1 constant:2]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:200]];
It results in the same constraints, but takes up twice the space and is harder to read.So I strongly recommend you to use visual format instead. You should only use constraintWithItem: for more complicated layout which visual format can't always handle.

UITableViewController: programmatically autolayout bug on iOS 7

I want to have a UIView programmatically added to a UITableViewController which is using autolayout...
I have the following code inside of the viewDidLoad of a UITableViewController:
UIView *centerView = [[UIView alloc] init];
[centerView setTranslatesAutoresizingMaskIntoConstraints:NO];
centerView.backgroundColor = [UIColor greenColor];
[self.view addSubview:centerView];
// Width constraint, half of parent view width
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeWidth
multiplier:0.5
constant:0]];
// Height constraint, half of parent view height
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:0.5
constant:0]];
// Center horizontally
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
// Center vertically
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
If I now run the app on iOS 8 all working well:
If I run it on iOS 7, the app crashes with the following error:
*** Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2935.137/UIView.m:8794
If I set setTranslatesAutoresizingMaskIntoConstraints to YES, the app crashes as well with this notice: Unable to simultaneously satisfy constraints
Okay, it looks like UITableView doesn't support Autolayout on iOS 7. So we have to use autoresizing masks. My idea is to put a containerView with autoresizing mask as the tableview's subview and inside that your centerView:
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupBackgroundView];
}
- (void)setupBackgroundView
{
UIView *backgroundContainerView = [[UIView alloc] initWithFrame:CGRectZero];
backgroundContainerView.backgroundColor = [UIColor cyanColor];
backgroundContainerView.frame = self.tableView.bounds;
backgroundContainerView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:backgroundContainerView];
UIView *centerView = [[UIView alloc] initWithFrame:CGRectZero];
centerView.translatesAutoresizingMaskIntoConstraints = NO;
centerView.backgroundColor = [UIColor greenColor];
[backgroundContainerView addSubview:centerView];
[backgroundContainerView addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:backgroundContainerView
attribute:NSLayoutAttributeWidth
multiplier:0.5
constant:0]];
// Height constraint, half of parent view height
[backgroundContainerView addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:backgroundContainerView
attribute:NSLayoutAttributeHeight
multiplier:0.5
constant:0]];
// Center horizontally
[backgroundContainerView addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:backgroundContainerView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
// Center vertically
[backgroundContainerView addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:backgroundContainerView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
}
Now centerView moves when the table view gets scrolled. If you want to have it fixed and the content hovering above, you should use backgroundView on UITableView:
- (void)setupBackgroundView
{
UIView *backgroundContainerView = [[UIView alloc] initWithFrame:CGRectZero];
backgroundContainerView.backgroundColor = [UIColor cyanColor];
UIView *centerView = [[UIView alloc] initWithFrame:CGRectZero];
centerView.translatesAutoresizingMaskIntoConstraints = NO;
centerView.backgroundColor = [UIColor greenColor];
[backgroundContainerView addSubview:centerView];
[backgroundContainerView addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:backgroundContainerView
attribute:NSLayoutAttributeWidth
multiplier:0.5
constant:0]];
// Height constraint, half of parent view height
[backgroundContainerView addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:backgroundContainerView
attribute:NSLayoutAttributeHeight
multiplier:0.5
constant:0]];
// Center horizontally
[backgroundContainerView addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:backgroundContainerView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
// Center vertically
[backgroundContainerView addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:backgroundContainerView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
self.tableView.backgroundView = backgroundContainerView;
}
Try removing this line from your code once and build then.
[centerView setTranslatesAutoresizingMaskIntoConstraints:NO];

All images of UIScrollView are getting placed in the beginning

I am new to iOS programming. I want to generate all the controls using coding and then apply constraints to achieve autosize feature. I had achieved almost my requirement except for one problem and that is all images of my UIScrollView are getting placed at very beginning and rest of the UIScrollView stays empty. I think I am having some sort of problem with my constraints and currently I am not able to resolve it.
This is my code
self.bgView.image = [UIImage imageNamed:#"bg.png"];
NSDictionary *viewDictionary = #{#"bgImage":self.bgView,#"scrollView":self.scrollView};
NSDictionary *position = #{#"vSpacing":#0,#"hSpacing":#0};
//here I had specified the size of the background image corresponding to the view
NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-hSpacing-[bgImage]-hSpacing-|" options:0 metrics:position views:viewDictionary];
NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-vSpacing-[bgImage]-vSpacing-|" options:0 metrics:position views:viewDictionary];
[self.view addConstraints:constraint_POS_H];
[self.view addConstraints:constraint_POS_V];
//here I am specifying the size of scroll view
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:self.scrollView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.bgView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:self.scrollView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.bgView
attribute:NSLayoutAttributeHeight
multiplier:0.5
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:self.scrollView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.bgView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:self.scrollView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.bgView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
//self.view.autoresizesSubviews = YES;
self.scrollView.pagingEnabled = YES;
NSInteger numberOfViews = photoArray.count;
for (int i=0; i < numberOfViews; i++) {
CGFloat myOrigin = i * self.view.frame.size.width;
NSLog(#"self.view.frame.size.width : %f",self.view.frame.size.width);
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, self.scrollView.frame.size.height)];
[myView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:myView];
//here I am specifying the size of uiview
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:myView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.scrollView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:myView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0.0]];
//here I am specifying the position of uiview
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:myView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:myView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
UIImageView *photos = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, myView.frame.size.width, self.scrollView.frame.size.height)];
//self.photos = [UIImageView new];
[photos setTranslatesAutoresizingMaskIntoConstraints:NO];
photos.image = [photoArray objectAtIndex:i];
[myView addSubview:photos];
//here I am specifying the size of image view within scroll view
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:photos
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationLessThanOrEqual
toItem:myView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:photos
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0.0]];
//here I am specifying the position of the image view
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:photos
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:photos
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:myView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
self.scrollView.delegate = self;
[self.scrollView addSubview:myView];
NSLog(#"self.myView.frame.size.width : %f",myView.frame.size.width);
}
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews,
self.scrollView.frame.size.height);
CGPoint scrollPoint = CGPointMake(0, 0);
[self.scrollView setContentOffset:scrollPoint animated:YES];
[self.view addSubview:self.scrollView];
you can easily solve it by just using reset to suggested constraints in storyboard.First select viewController and then press right bottom menu and select reset to suggested constraints in All views tab
This worked for me.

Resources