I have setup a viewcontroller using storyboard + autolayout, however I am having some problems (re)sizing a subview correctly for 3.5"/4" display.
This is what I have setup in storyboard:
The scrollview correctly keeps its 20px space to the bottom according to the constraint. However the subviews (red), which I add in the below loop to the scrollview should have the same height as the scrollview, but they don't :(
int pageCount= 3;
self.scrollView.delegate= self;
self.scrollView.pagingEnabled= YES;
self.scrollView.scrollEnabled= YES;
self.scrollView.showsHorizontalScrollIndicator= NO;
self.scrollView.showsVerticalScrollIndicator= NO;
self.scrollView.contentSize= CGSizeMake(self.scrollView.bounds.size.width * pageCount, self.scrollView.bounds.size.height);
NSLog(#"viewcontroller frame: %# (bounds: %#)", NSStringFromCGRect(self.view.frame), NSStringFromCGRect(self.view.bounds));
NSLog(#"scrollview frame: %# (bounds: %#)", NSStringFromCGRect(self.scrollView.frame), NSStringFromCGRect(self.scrollView.bounds));
NSLog(#"scrollview content size%#", NSStringFromCGSize(self.scrollView.contentSize));
for (int i= 0; i < pageCount; i++)
{
UIView* neutralContainer= [[UIView alloc] initWithFrame:CGRectMake(0.0 + i * self.scrollView.frame.size.width, 0.0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
neutralContainer.backgroundColor= [UIColor redColor];
NSLog(#"containerview frame: %# (bounds: %#)", NSStringFromCGRect(neutralContainer.frame), NSStringFromCGRect(neutralContainer.bounds));
[self.scrollView addSubview:neutralContainer];
As it seems, the scrollview frame i print in console (see screenshot) remains the same, no matter if i start the 4" or the 3.5" simulator. i would expect to get different values.
Can someone explain to me what i am doing wrong?
Try setting your scrollview content size.
self.scrollView.contentSize.Height = self.scrollView.frame.size.height;
Your subview is being added dynamically, and does not have to conform to its parent's constraints.
Coming across this post Storyboard UIScrollView contentSize? helped me solve the problem:
Instead of initializing the subviews for UIScrollview in the viewDidLoad method, I moved the code to viewDidLayoutSubviews and voila, the content size is correct for both 3.5" and 4", and the subviews are layed out correctly within my scrollview.
Related
My app uses storyboards and autolayout. In a view controller I created a UIScrollView and added three subviews.
At run time I need to change the sizes of the subviews and rearrange them. Initially, I rearrange them in viewDidLayoutSubviews, and it seems to work until I scroll the scroll view. Then the subview frames are changed back to the sizes that are set in IB. (I'm setting the UIScrollView's content size when rearranging the views and removing all UIViewConstraints.)
Frames and Auto Layout doesn't go together, If you are using Auto Layout you'll need to rearrange the constrains, not the frames. While using Auto Layout Changing the frames will cause some weird effects and will eventually revert back to the constraints you've created in the original UIView.
A few solutions:
You can create an outlet to each constrain just like you would to a view and change its constant when needed.
If you are using xib you can disable Auto Layout in that specific xib.
i am using auto layout. and working properly. i have a uiscrollview and it contains 3-4 images. it depends to how many images there is. when i try to make fullscreen current image, i am using a functioni hope it works.its like this:
-(void) growImg {
if (self.growImgNeed) {
self.Scroller.frame=CGRectMake(0, 100, 320, 376);
CGRect ViewSize = Scroller.bounds;
for(int i=0;i< Scroller.subviews.count;i++){
[[self.Scroller.subviews objectAtIndex:i] setSize:CGSizeMake(320, 376)];
[[self.Scroller.subviews objectAtIndex:i] setFrame:ViewSize];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
}
Scroller.contentSize = CGSizeMake((Scroller.subviews.count) * Scroller.bounds.size.width, Scroller.bounds.size.height);
self.growImgNeed=false;
}
else {
self.Scroller.frame=CGRectMake(80, 82, 160, 188);
CGRect ViewSize = Scroller.bounds;
for(int i=0;i< Scroller.subviews.count;i++){
[[self.Scroller.subviews objectAtIndex:i] setSize:CGSizeMake(160, 188)];
[[self.Scroller.subviews objectAtIndex:i] setFrame:ViewSize];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
}
Scroller.contentSize = CGSizeMake((Scroller.subviews.count) * Scroller.bounds.size.width, Scroller.bounds.size.height);
self.growImgNeed=true;
}
}
I need your help / suggestions guys.
I'm using a UIScrollView (horizontal) to show some data. There is a custom UIView inside this scrollView, made in Interface Builder. I'm not using Auto layout (maybe I should... I tried, but it's doesn't look as I want, or I don't understand how it works...).
Well, here is my code where my scrollView is powered:
- (void)loadList
{
for (int i = 0; i < [DBManager getSharedInstance].getCountItems; i++) {
Item *item = [[DBManager getSharedInstance] getCachedItemAtIndex:i];
PanelPage *page = [[PanelPage alloc] initWithItem:item];
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
page.view.frame = frame;
[self.scrollView addSubview:page.view];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * [DBManager getSharedInstance].getCountItems, self.scrollView.frame.size.height);
self.pageControl.numberOfPages = [DBManager getSharedInstance].getCountItems;
}
It works good. The size of the UIView inside the scrollView :
Height: 397px
Width: 285px
I have a UINavigationBar (64px height) and a UITabBar (44px height), working on iOS 7
For screen of 4 inches, it's perfect, but on 3,5 inches, I would like to resize the view insides my scrollView, but I don't know how to proceed. A tried many things but no good results :/
Thank you so much for your help.
Any suggestion is appreciate ;)
EDIT:
Well, I can now resize my scrollview when size screen change. See: https://stackoverflow.com/a/15401903/2061450
Searching for resize the views inside the scrollview now...
You should call loadList function in viewDidAppear, not before (e.g. in viewDidLoad).
I have set up a UIScrollView with which I want to display 12 images (only 8 fit on screen) laid out horizontally. In the following image you can see the problem I'm having (which makes my scroll view not scroll), my constraints and the UIScrollView which I have added on storyboard:
I have called the following method on -(void)viewDidLoad, where I "set up"my scrollview (itemList is my scroll view property and itemNames a array with the images'names):
- (void)setupHorizontalScrollView
{
self.itemList.delegate = self;
[self.itemList setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.itemList setBackgroundColor:[UIColor blackColor]];
[self.itemList setCanCancelContentTouches:NO];
self.itemList.indicatorStyle = UIScrollViewIndicatorStyleWhite;
self.itemList.clipsToBounds = NO;
self.itemList.scrollEnabled = YES;
self.itemList.pagingEnabled = NO;
NSInteger tot=0;
CGFloat cx = 0;
for (; ; tot++) {
if (tot==12) {
break;
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[self.itemNames objectAtIndex:tot]]];
CGRect rect = imageView.frame;
rect.size.height = 40;
rect.size.width = 40;
rect.origin.x = cx;
rect.origin.y = 0;
imageView.frame = rect;
[self.itemList addSubview:imageView];
cx += imageView.frame.size.width;
}
[self.itemList setContentSize:CGSizeMake(cx, [self.itemList bounds].size.height)];
}
I have added the [self.itemList setTranslatesAutoresizingMaskIntoConstraints:NO]; because I saw this suggestion on other posts, but it doesn't work with or without it. The only way it works is if I uncheck use AutoLayout on the storyboard, but that moves the UIImageViewI use to look as a navigation bar to the bottom of the screen.
I don't know what to do anymore, any help is appreciated :)
Try to set your scrollView's Content size int "viewDidLayoutSubviews" method with keeping the autolayouts set.
-(void)viewDidLayoutSubviews
{
[self.itemList setContentSize:CGSizeMake(required_width, required_height)];
}
Two Solutions:
Create different constraints that can be satisfied simultaneously (you will have to edit). I think the problem is your bottom space and top space constraints are mutually exclusive. please remove one and try again. IF this is difficult for you, try adding another UIView to contain the UIScrollView to help manage your constraints, it might seem odd at first, but sometimes adding another view to contain your view actually makes it simpler at each level.
Turn off Autolayout, and change the autoresize masks of your UIImageView to be what you wish.
Insert: [scrollView setContentSize:CGSizeMake(x,y)]; in the following method:
-(void)viewWillAppear:(BOOL)animated
I have a UILabel in the main view with text - "Very Very long text". The proper width to this would be 142, but i've shortened it to 55.
Basically I want to implement a marquee type scroll, so I wrote code to add it onto a subview and animate it within the bounds of that view.
CODE --
CGRect tempLblFrame = _lblLongText.frame;
UIView *lblView = [[UIView alloc] initWithFrame:tempLblFrame];
//Add label to UIView at 0,0 wrt to new UIView
tempLblFrame.origin.x = 0;
tempLblFrame.origin.y = 0;
[_lblLongText setFrame:tempLblFrame];
[_lblLongText removeFromSuperview];
[lblView addSubview:_lblLongText];
//SetClipToBounds so that if label moves out of bounds of its superview, it wont be displayed
[lblView setClipsToBounds:YES];
[lblView setBackgroundColor:[UIColor cyanColor]];
[self.view addSubview:lblView];
After this I get this output on the simulator -->
The problem occurs when i try the Animation with this code -
tempLblFrame.origin.x = -_lblLongText.intrinsicContentSize.width;
[UIView animateWithDuration:2.0 delay:1.0 options:UIViewAnimationOptionCurveLinear
animations:^{
[_lblLongText setFrame:tempLblFrame];
}
completion:^(BOOL finished) {
NSLog(#"completed");
}];
I was hoping I would see the entire "Very Very long text", rather only "Very..." scrolls from left to right.
To solve this I added one line of code --
//Add label to UIView at 0,0 wrt to new UIView
tempLblFrame.origin.x = 0;
tempLblFrame.origin.y = 0;
tempLblFrame.size.width = _lblLongText.intrinsicContentSize.width; //THIS LINE WAS ADDED
[_lblLongText setFrame:tempLblFrame];
[_lblLongText removeFromSuperview];
[lblView addSubview:_lblLongText];
I thought the full text will be set inside the newly added UIView and it would scroll properly. But running in the simulator gave me this --
And again, only "Very..." was scrolling from left to right.
What am I doing wrong? Please help!!
EDIT
Apparently the culprit was AutoLayout.
I have no clue why, but once I unchecked "Use Autolayout" for the view
in the XIB, everything started working as expected. Setting
tempLblFrame.origin.x = -_lblLongText.intrinsicContentSize.width; was
working properly and so was the scroll.
Any explanation on this!!?
This question is possibly Duplicate of.
Although there is nice code snippet written by Charles Powell for MarqueeLabel,
also take a look at This link.
I hope this will help you and will save your time by giving a desired output.
Make the UILabel the width (or longer) of the text and the UIView the scroll area you want to see. Then set the UIView's clipToBounds to YES (which you are doing). Then when you animate left to right you will only see the the text the width of the UIView, since it is cutting any extra subviews. Just make sure you scroll the entire length of the UILabel.
Right now you are setting the view and label's height and width to the same thing. This is why you are getting clipped text, not a clipped label.
You add In your view scrollview and add this label in your scroll view .Use this code
scroll.contentSize =CGSizeMake(100 *[clubArray count],20);
NSString *bname;
bname=#"";
for(int i = 0; i < [clubArray count]; i++)
{
bname = [NSString stringWithFormat:#"%# %# ,",bname,[[clubArray objectAtIndex:i] objectForKey:#"bottle_name"]];
[bname retain];
}
UILabel *lbl1 = [[UILabel alloc] init];
[lbl1 setFrame:CGRectMake(0,5,[clubArray count]*100,20)];
lbl1.backgroundColor=[UIColor clearColor];
lbl1.textColor=[UIColor whiteColor];
lbl1.userInteractionEnabled=YES;
[scroll addSubview:lbl1];
lbl1.text= bname;
This is implemented code.Thanks
Apparently the culprit was AutoLayout.
I have no clue why, but once I unchecked "Use Autolayout" for the view in the XIB, everything started working as expected. Setting tempLblFrame.origin.x = -_lblLongText.intrinsicContentSize.width; was working properly and so was the scroll.
Still, a better explanation for this would surely help!!
EDIT: Solution with AutoLayout -
//Make UIView for Label to sit in
CGRect tempLblFrame = _lblLongText.frame;
UIView *lblView = [[UIView alloc] initWithFrame:tempLblFrame];
//#CHANGE 1 Removing all constraints
[_lblLongText removeConstraints:_lblLongText.constraints];
//Add label to UIView at 0,0 wrt to new UIView
tempLblFrame.origin.x = 0;
tempLblFrame.origin.y = 0;
//Set Full length of Label so that complete text shows (else only truncated text will scroll)
tempLblFrame.size.width = _lblLongText.intrinsicContentSize.width;
//#CHANGE 2 setting fresh constraints using the frame which was manually set
[_lblLongText setTranslatesAutoresizingMaskIntoConstraints :YES];
[_lblLongText setFrame:tempLblFrame];
[_lblLongText removeFromSuperview];
[lblView addSubview:_lblLongText];
I am getting strange behavior with UIScrollView subviews, the idea is to create programmatically an instance of UIView with a customized nib file which is a form in my case, fill that form with data from a model class, and add it as subview for my UIScrollView. The problem is when I deal with more than one subview, the UIScrollView only keep the latest subview, so if I created three subviews, the scrollview will show only the third (the latest) subview. Although the page control is set to the coreect number of subviews (three).
The project is too long, so I will try to explain brievely my issue with the relevant code:
- (void)viewDidLoad {
NSArray *sorted = [appArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];//this array contains the data from model, I debugged that to make sure I got the exact data, no more no less :)
//Loop all NSManaged objects, for example let's say I have 3 model objects, don't worry about how I get data etc, because I debugged all and maked sure all data objects number are exact, etc.
for (App_Table *_appTable in sorted) {
//This looped 3 times as expected, I debugged that also and maked sure on each iteration I got the data I expected to have
//App_Table is a subclass of NSManagedObject, it's my model class and get its data from coredata file
[self addMoreView];//Call this method will create a new subview and add it as subview to the UIScrollView, it will also update the page control, update the content size property, etc.
AppTableView *_appTableView = (AppTableView *) [[self.scrollView subviews] lastObject];//Remember addMoreView method create a new instance of AppTableView and add it as subview for the UIScrollView property, then I get that subview to fill it with data here
_appTableView.txtADDRESS.text = _appTable.address;//Fill in the form, no need to write all the form fields code because it's the same way.
// Scroll To First Page...
self.pageControl.currentPage = 0;
[self.scrollView scrollRectToVisible:CGRectMake(0, 0, viewWidth, viewHeight) animated:YES];
self.scrollView.contentSize = CGSizeMake(viewWidth*noOfItems, viewHeight);//Set the content size to the sum of subviews width, I also debugged that to check it's correct
self.scrollView.delegate = self;
[super viewDidLoad];
}
Ok, so when I have three subviews, the scrollview will load with the width of three subviews as calculated with the line above:
self.scrollView.contentSize = CGSizeMake(viewWidth*noOfItems, viewHeight);//Set the content size to the sum of subviews width, I also debugged that to check it's correct
And I can scroll till 3 moves (which is the number of subviews) and the UIPageControl is also set to three dots, but only ONE subview is visible, only the latest one I can see, the two other subviews disappeared, the scroll view calculated content size for them but they are not visible. Any thoughts ? Thanx.
EDIT:
It's worth to note that the first time I edit the view, all goes fine, when I deal with 3 subviews, they are all visible, but when I go to another view and get back to this view, only the last subview is visible.
Also, I am working on an iPad project for that with a split view.
EDIT:
This is the code of the method which draw new subview for the UIScrollView
-(IBAction) addMoreView
{
NSArray *arr = [[NSBundle mainBundle] loadNibNamed:#"AppTableView" owner:self options:nil];
AppTableView *aView = [arr objectAtIndex:0];
[aView setFrame:CGRectMake(startX, 0, aView.bounds.size.width, aView.bounds.size.height)];
[self.scrollView addSubview:aView];
self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width+aView.frame.size.width
, self.scrollView.contentSize.height);
startX = startX + aView.frame.size.width;//Update the X position, first 0, then 600, 1200, and so on.
[self.scrollView scrollRectToVisible:aView.frame animated:YES];
NSLog(#"%f",self.scrollView.contentSize.width);
NSLog(#"%f",self.scrollView.contentSize.height);
}
Suppose I have 3 subviews, the method above will be called 3 times since it's put inside the for loop. So here is the result of NSLogs for the above method:
NSLog(#"%f",self.scrollView.contentSize.width);
NSLog(#"%f",self.scrollView.contentSize.height);
First iteration:
600.000000
0.000000
Second iteration:
1200.000000
0.000000
Third iteration:
1800.000000
0.000000
EDIT:
The command suggested by rob mayoff allows me to see why this happen:
| | | | <AppTableView: 0x1eef4700; frame = (0 18; 600 430); autoresize = LM+RM+TM+BM; tag = 990; layer = <CALayer: 0x1eef4790>>
| | | | <AppTableView: 0x1ee3f380; frame = (0 18; 600 430); autoresize = LM+RM+TM+BM; tag = 991; layer = <CALayer: 0x1ee3f410>>
| | | | <AppTableView: 0x1ee56910; frame = (0 18; 600 430); autoresize = LM+RM+TM+BM; tag = 992; layer = <CALayer: 0x1ee3f410>>
All Three subviews are drawn in the same frame, so they are above each other, but when I debug that with breakpoints in runtime, the x is changing, first time 0, then 600 then 1200 which make me think it's drawing correctly. How to fix that especially that the x value is being incremented correctly, so what's the problem and why they still drawing on the same x coordinate?
First things first, you should place the [super viewDidLoad] call at the top of your - (void)viewDidLoad method, not at the bottom.
Given that it's not clear enough what you are actually seeing and what you expect to see, providing a minimum working example of your problem as a downloadable project would help us to help you. If you just try to reproduce this in a separate view controller that does not interact with managed objects, but uses some statically provided data, others will be able to reproduce it themselves and debug it. Or you might just as well figure it out yourself in the process.
I am getting back a bit late, but finally I figure out the fix for my problem so thought it's good to share it to save someone else's time.
For my case, the scrollview is a custom view in a nib file. So by activating its Autosizing masks left and top, the bug was fixed.
This block of code in -addMoreView is the problem:
AppTableView *aView = [arr objectAtIndex:0];
[aView setFrame:CGRectMake(startX, 0, aView.bounds.size.width, aView.bounds.size.height)];
[self.scrollView addSubview:aView];
If startX never changes, you're adding all of these views at the same spot.
Surely your not setting the content size correctly for 3 items:
self.scrollView.contentSize.width+aView.frame.size.width
should be
self.scrollView.contentSize.width+ ( aView.frame.size.width * NUMBER_OF_SUBVIEWS)
I presume when you scroll horizontally on the scrollview, the content size only allows enough room for one subview correct ?
used a static variable which is reinitialized to 0 on load, then it gets incremented by the width of the subview to start drawing on new X position.
Try to set the autoresizingMask of aView to UIViewAutoresizingNone inside the addMoreView method.
I created a project that uses your code in the sister post, and its working just fine. I added several tests on the autoresizing masks as you can see below. I suggest you compare it to what you are doing. Weird things happen when the masks on EITHER the subviews or the scrollView are not set to being tied to the top/left.
My modified code:
NSArray *colors = #[ [UIColor redColor], [UIColor greenColor], [UIColor blueColor] ];
UIViewAutoresizing mask;
mask = [self.scrollView autoresizingMask];
//assert(!self.scrollView.autoresizesSubviews); // has no affect here
if((mask & (UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin)) != mask) NSLog(#"scrollView MASK WRONG");
for (int i=0; i<3; ++i) {
NSArray *arr = [[NSBundle mainBundle] loadNibNamed:#"AppTableView" owner:self options:nil];
NSLog(#"scrollView frame: %#", NSStringFromCGRect(self.scrollView.frame));
if((mask & (UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin)) != mask) NSLog(#"aView MASK WRONG");
AppTableView *aView = [arr objectAtIndex:0];
assert([aView isKindOfClass:[AppTableView class]]);
NSLog(#"orig aView frame: %#", NSStringFromCGRect(aView.frame));
UIViewAutoresizing mask = [aView autoresizingMask];
if((mask & (UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin)) != mask) NSLog(#"aView MASK WRONG");
aView.frame = CGRectMake(startX, 0, aView.bounds.size.width, aView.bounds.size.height);
NSLog(#"changed aView frame: %#", NSStringFromCGRect(aView.frame));
aView.backgroundColor = colors[i];
[self.scrollView addSubview:aView];
self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width+aView.frame.size.width
,self.scrollView.contentSize.height);
startX = startX + aView.frame.size.width;
//AppTableView *_appTableView = (AppTableView *) [[self.scrollView subviews] lastObject];
//_appTableView.txtADDRESS.text = _appTable.address;//Fill in the form, no need to write all the form fields code because it's the same way.
}
NSLog(#"scrollView frame: %#", NSStringFromCGRect(self.scrollView.frame));
NSLog(#"scrollView contentOffset: %#", NSStringFromCGPoint(self.scrollView.contentOffset));