UIView inside UIScrollView not resizing on 3,5 inches screen iOS - ios

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).

Related

UIScrollView Lazy Loading Images

My app for the iPhone Contains 170 images. I read them into an array, and in the loop add a picture in the viewfinder, then put the image View as sub view of scroll view.
When running my app uses too much memory 700mb. I tried to reduce the size of the pictures, but it didn't work.
One of my friends said that I should add only images # 1 and # 2. When the user block is scrolled to the picture No. 1, then only show the picture No. 2. Then the picture No. 1 to remove from the image viewer and add the picture No. 3.
He says that in this way I can maintain the normal memory consumption. But I don't understand how to do this?
Could you help with an example? Thanks in advance.
Not using UICollectionView
Here is my code:
- (void)addImageViewToScrollView {
_assetsArray = [[NSMutableArray alloc] init];
for (int j = 0; j < 170; j++) {
[_assetsArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"%d",j]]];
}
//scrollView add subview ImageView
for (int i = 0; i < [_assetsArray count]; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width *i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
_imageView = [[UIImageView alloc]init];
_imageView.image = [_assetsArray objectAtIndex:i];
_imageView.frame = frame;
[self.scrollView addSubview:_imageView];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * self.assetsArray.count, self.scrollView.frame.size.height);
}
--
Use a UICollectionViewController to solve this issue.
This way only the cells that are required on screen are loaded and all others images are can be popped from cache when you hit a memory warning.
Collection Views work very similar to table views.
To get the collection view to suit what you're doing you'll need to set the flow layout to horizontal scrolling and set the cell sizes to your view's height and width.
Seems like you are trying to add170images onto UIScrollView using UIImageView. Why not use dedicated UITableView's or UICollectionView's? Object of Reusable Cell is to maintain run-time memory consumption. Let's recap on this,
You have 170 ImageViews (_assetsArray) ; expensive consumption
You have 170 Images
Versus, Using UITableView or UICollectionView
You have 1 ImageView in a Cell with 170 rows
You have DataSource of 170 Images were loaded
Your cell will be re-cycled when it needed to display correspond to DataSource
Also, for supporting the multitude images, there are good libraries to reduce images cache. Try to use SDWebImage or FastImageCache. I personally like FIC but you might need to understand the concept.

Issues with autolayout and 3.5" vs 4"

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.

Scrolling a UIScrollView changes its subviews frames

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;
}
}

iOS: Autolayout causing UIScrollView to not scroll

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

ios create 'readmore'. Let superview be dependent of subview

gaaah designing in ios gives me headache!!
So please, help me out and maby explain to me how one should think trying to come up with the solution.
I have:
As you can see the UITextField has its frame being set in storyboard to be smaller than its actual content.
Everything above the prototypecell is within a UIView that is set to be tableHeader programatically.
I want to:
Press the read more btn so that the UITextField get its actual size. No problem, I can do that with getting the contentSize programmatically. It works but it ofc overflows the tableHeader
So I thought, good. Then all I have to do is set the tableHeader to be the size of the 'new' calculated height of UITextField + height of the 2 UIImageViews.
But nope. it only resizes to the existing height set in storyboard insted. In other word, it does one or the other.
And using autolayout it totally breaks but not giving me any errors about constraints.
This seems so easy wich makes me feel so stupid haha
this is what I have i code
- (IBAction)toggleReadMore:(id)sender{
_toggleReadMoreBtn.hidden = YES;
CGRect textFrame = _cityDescription.frame;
_cityDescription.frame = textFrame;
CGRect tableHeaderViewFrame = CGRectMake(0, 0, self.tableView.tableHeaderView.frame.size.width, _cityDescription.contentSize.height + 218.0f ); //textFrame.size.height
self.tableView.tableHeaderView.frame = tableHeaderViewFrame;
textFrame.size.height = _cityDescription.contentSize.height;
[self.tableView setTableHeaderView:self.viewForTableHeader];
please, guide me how to think
- (IBAction)readMoreBtnClicked:(UIButton *)sender
{
NSLog(#"Read more Btn Clicked");
NSString *stringToBeDisplayed = #"Any Text Here";
CGSize textSize=[stringToBeDisplayed sizeWithFont:[UIFont systemFontOfSize:30]
constrainedToSize:CGSizeMake(270, 500)
lineBreakMode:NSLineBreakByWordWrapping];
NSLog(#"textSize = %#",NSStringFromCGSize(textSize));
[self.textView setFrame:CGRectMake(self.textView.frame.origin.x,self.textView.frame.origin.y, textSize.width, textSize.height)];
NSLog(#"self.textView.frame = %#",NSStringFromCGRect(self.textView.frame));
[self.textView setText:stringToBeDisplayed];
[self.headerView setFrame:CGRectMake(self.headerView.frame.origin.x,self.headerView.frame.origin.y, 320, dynamicHeightCalculatedAfterTextSize)];
[self.tableView reloadData];
}

Resources