Create UILable at runtime and set autolayout programatically related to each other - ios

I have a UIView in UITableViewCell. I am getting a array of names from service and I want to create a UILabel in a for loop inside a UIView which is in UITableViewCell according to the text width.
I have done this at my end but the problem is that I want to check if the label which is being created is having a width greater than the space remaining in its superview, then it should be created below the first line just like word wrapping of text in UILabel.
What I have done is, I have created a sample of code
-(void)addADaynamicViw
{
[self.vwContainer setTranslatesAutoresizingMaskIntoConstraints:false];
UILabel *lblPrevious;
int totalWidth;
for (int i=0; i<8; i++) {
UILabel *lbl=[[UILabel alloc]init];
//[lbl setFrame:CGRectMake(0, 0, 30, 50)];
[lbl setNumberOfLines:0];
[lbl setBackgroundColor:[UIColor greenColor]];
[lbl setTranslatesAutoresizingMaskIntoConstraints:false];
[lbl setText:[NSString stringWithFormat:#"Label Num %d",i]];
[self.vwContainer addSubview:lbl];
NSLayoutConstraint *top;
top=[NSLayoutConstraint constraintWithItem:lbl attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.vwContainer attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
NSLayoutConstraint *left;
if (lblPrevious) {
CGSize size = lblPrevious.intrinsicContentSize;
NSLog(#"intrinzik content size =%f",size.width);
left =[NSLayoutConstraint constraintWithItem:lbl attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationLessThanOrEqual toItem:lblPrevious attribute:NSLayoutAttributeLeft multiplier:1 constant:size.width+2];
}
else
{
left=[NSLayoutConstraint constraintWithItem:lbl attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationLessThanOrEqual toItem:self.vwContainer attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
}
NSLayoutConstraint *width=[NSLayoutConstraint constraintWithItem:lbl attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:10];
NSLayoutConstraint *height=[NSLayoutConstraint constraintWithItem:lbl attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:50];
NSLayoutConstraint *bottom=[NSLayoutConstraint constraintWithItem:lbl attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.vwContainer attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
NSLayoutConstraint *right=[NSLayoutConstraint constraintWithItem:lbl attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationLessThanOrEqual toItem:self.vwContainer attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0];
[self.vwContainer addConstraint:left];
[self.vwContainer addConstraint:bottom];
[self.vwContainer addConstraint:width];
[self.vwContainer addConstraint:top];
[self.vwContainer addConstraint:height];
[self.vwContainer addConstraint:right];
lblPrevious=lbl;
CGSize size = lblPrevious.intrinsicContentSize;
totalWidth+=size.width;
}
[self.vwContainer layoutIfNeeded];
[self.vwContainer updateConstraintsIfNeeded];
}
This line of code is creating the label programmatically after the end of previous label, but I want that if the label which is being created does not fit in its superview then it should increase the height of its superview and should start from 0 of its superview, but below the first label.

Related

Unable to add textfields vertically in iOS?

I have added some text fields through story board vertically.Below them i want to add more text fields programatically but it is always adding only one text field.
Below is the code used
int i;
self.main_view.translatesAutoresizingMaskIntoConstraints = NO;
self.scroll_view.translatesAutoresizingMaskIntoConstraints = NO;
for (i=0; i<[arr_customEdtTxt count]; i++)
{
JVFloatLabeledTextField *tf = [[JVFloatLabeledTextField alloc] initWithFrame:CGRectMake(25, y, 270, 27)];
tf.textColor = [UIColor blackColor];
tf.font = [UIFont fontWithName:#"Helvetica" size:14];
CustomEditText *custom=[arr_customEdtTxt objectAtIndex:i];
tf.placeholder=custom.field_name;
tf.textAlignment=NSTextAlignmentLeft;
previousTextfield=tf;
//add bottom border
[self addBorder:tf];
tf.translatesAutoresizingMaskIntoConstraints = NO;
[self.main_view addSubview:tf];
y=y+27;
[self.main_view addConstraint:[NSLayoutConstraint constraintWithItem:tf attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.main_view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0] ];
[self.main_view addConstraint:[ NSLayoutConstraint constraintWithItem:tf
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:27.0]];
[self.main_view addConstraint:[ NSLayoutConstraint constraintWithItem:tf
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.txt_email
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0]];
if(i==0)
{
[self.main_view addConstraint:[NSLayoutConstraint constraintWithItem:tf attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.txt_exprience attribute:NSLayoutAttributeBottom multiplier:1 constant:15.0f]];
}
else
{
[self.main_view addConstraint:[NSLayoutConstraint constraintWithItem:tf attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:previousTextfield attribute:NSLayoutAttributeBottom multiplier:1 constant:15.0f]];
}
[arr_tf addObject:tf];
}
[self.main_view layoutIfNeeded];
[self setDatatoFields];
I am getting constraint error i want to add more text fields below the current text fields.Please tell how can i add more and more text fileds below
It looks like previousTextfield and tf refer to the same textfield in this line:
[self.main_view addConstraint:[NSLayoutConstraint constraintWithItem:tf attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:previousTextfield attribute:NSLayoutAttributeBottom multiplier:1 constant:15.0f]];
Also I noticed that at the end you add the textField to arr_tf but you also have arr_customEdtTxt depending on which one is your array of textFields you should change previousTextfield = tf with:
if(i > 0){
//Change arr_tf for arr_customEdtTxt if that is your array
previousTextfield = [arr_tf objectAtIndex:i-1];
}

Adding imageview on UITextField with auto layout

I am creating a custom searchbar with UITextfield. Searchbar should look like this:
But, In my case it is appearing like this:
What is the mistake I am doing in setting constraints for imageview on textfield
Code:
UITextField *searchBarTF=[[UITextField alloc]init];
[searchBarTF setBackgroundColor:[UIColor whiteColor]];
searchBarTF.layer.borderColor=[[UIColor darkGrayColor]CGColor];
searchBarTF.layer.borderWidth=7;
searchBarTF.layer.cornerRadius=5;
searchBarTF.returnKeyType=UIReturnKeySearch;
searchBarTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Search by locations" attributes:#{NSForegroundColorAttributeName: [UIColor grayColor],NSFontAttributeName:[UIFont fontWithName:#"Helvetica" size:16]}];
searchBarTF.textAlignment=NSTextAlignmentCenter;
UIImageView *srchBariconImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"srchBarIcon"]];
[srchBariconImageView setTranslatesAutoresizingMaskIntoConstraints:NO];
[searchBarTF addSubview:srchBariconImageView];
[searchBarTF addConstraint:[NSLayoutConstraint constraintWithItem:srchBariconImageView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:searchBarTF attribute:NSLayoutAttributeLeading multiplier:1.0f constant:40]];
[searchBarTF addConstraint:[NSLayoutConstraint constraintWithItem:srchBariconImageView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:searchBarTF attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-(views.frame.size.width-80)]];
[searchBarTF addConstraint:[NSLayoutConstraint constraintWithItem:srchBariconImageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:40]];
//
// [searchBarTF addConstraint:[NSLayoutConstraint constraintWithItem:srchBariconImageView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:40]];
[searchBarTF setTranslatesAutoresizingMaskIntoConstraints:NO];
// replace views with self.view.
[views addSubview:searchBarTF];
[views addConstraint:[NSLayoutConstraint constraintWithItem:searchBarTF attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:views attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
[views addConstraint:[NSLayoutConstraint constraintWithItem:searchBarTF attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:60]];
// [views addConstraint:[NSLayoutConstraint constraintWithItem:searchBarTF attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:views attribute:NSLayoutAttributeTop multiplier:1.0 constant:-64]];
[views addConstraint:[NSLayoutConstraint constraintWithItem:searchBarTF attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:views attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
[views addConstraint:[NSLayoutConstraint constraintWithItem:searchBarTF attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:views attribute:NSLayoutAttributeTop multiplier:1.0 constant:130]];
Subclass UITextField and overwrite these methods
- (void)awakeFromNib{
CGRect frame = self.leftView.frame;
frame.size.width = 30;
frame.size.height = 30;
self.leftView.frame = frame;
self.leftView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"underfloor_cross_blue.png"]];
}
- (CGRect)leftViewRectForBounds:(CGRect)bounds{
CGRect rect=CGRectMake(8, 8,50.0, 45);
[super leftViewRectForBounds:rect];
return rect ;
}
Note: change image and frame according to your need.

How to scroll the scroll view using auto-layouts "constraint with item" formate in ios?

Here I am adding one scrollview programmatically on Viewcontroller using "constraint with item" format and inside that scrollview I have added one UIlabel. So everything is good.
But here I have given "NSLayoutAttributeBottom" between scrollview and label is "-200" that means scrollview is scrolling up to "200" range on viewcontroller controller, but according to my code it is not scrolling.
As per drag and drop procedure it's ok, I have checked, it's working fine.
Why it's not possible to do it programmatically?
my code is below:-
#import "ViewController4.h"
#interface ViewController4 ()
{
UIScrollView * scrollView;
UILabel * label;
}
#end
#implementation ViewController4
- (void)viewDidLoad {
[super viewDidLoad];
scrollView = [[UIScrollView alloc] init];
scrollView.backgroundColor = [UIColor lightGrayColor];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:scrollView];
label = [[UILabel alloc] init];
label.backgroundColor = [UIColor redColor];
label.text = #"Hello World1";
label.textAlignment = NSTextAlignmentCenter;
label.translatesAutoresizingMaskIntoConstraints = NO;
[scrollView addSubview:label];
//Applying autolayouts for scrollview
NSLayoutConstraint * constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem: self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
[self.view addConstraint:constraint1];
constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
[self.view addConstraint:constraint1];
constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
[self.view addConstraint:constraint1];
constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
[self.view addConstraint:constraint1];
//Applying autolayouts for label
NSLayoutConstraint * constraint2 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem: scrollView attribute:NSLayoutAttributeTop multiplier:1.0f constant:100.0f];
[scrollView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f];
[scrollView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f];
[scrollView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30.0f];
[scrollView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f];
[scrollView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:-100.0f];
[scrollView addConstraint:constraint2];
}
In above code i have clearly given NSLayoutAttributeBottom space is "-200" but scroollview is not scrolling what did i do here wrong please help me some and how can i scroll this scrollview

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.

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