UICollectionView Set First Opening index Number - set

I have an UICollectionView. This CollectionView opens to index number "0" but I want it to open to index number "2" . How do I make it?

scrollView.setContentOffset(CGPoint(x: 0, y: cellHeight * 2), animated: false)
you can use this line in swift

Depends on your collectionViewcell height (If scrolling is vertically)
you can add [scrollView setContentOffset:CGPointMake(0,cellHeight *2) animated:NO];
in your viewWillApear

Related

Handling AutoLayout constraint animation differences in iOS 10?

I've noticed that in iOS 10 Beta 5 (about to try Beta 6), AutoLayout constraint animation behaves a bit differently.
For example, this approach does not work the same as it did in previous iOS releases:
[view addConstraints:#[constraints...]];
[view setNeedsUpdateConstraints];
[view layoutIfNeeded];
[UIView animateWithDuration:...
{
/* adjust constraint here... */
[view layoutIfNeeded]; // Only the adjusted constraints since previous layoutIfNeeded() call should animate change with duration.
} completion:{ ... }];
... In my testing, the constraints initially added with addConstraints() will also animate in iOS 10 with the UIView animateWithDuration() block... which is causing some funky/undesirable behavior so far.
For example, setting the left/right constraints in the array (but the vertical constraints in the block) causes the entire view to animate onto the screen diagonally with this approach... which is totally incorrect.
Does anyone know how to do this correctly for both iOS 9 (and below), as well as 10?
Try calling layoutIfNeeded on the view's superview, instead of the view itself.
--
I had a similar problem, my view was supposed to animate from the top at a 0 height, downward to a > 0 height (i.e. (0, 0, 320, 0) to (0, 0, 320, 44)).
Using Xcode 8 with iOS 10, the animation behavior was much different. Instead of animating downward from the top, the view animates up & down from the vertical center of the destination frame.
I fixed this by, instead of calling layoutIfNeeded on the view itself, calling it on the view's superview. Somehow, the behavior was restored.
Hope it helps!
Helpful comment by #Ramiro:
According to the documentation, layoutIfNeeded lays out the subviews (but doesn't contemplate the current view itself). So, in order to update the current view lay out, you need to call layoutIfNeeded from the super view.
In below method :
[super viewDidLoad];
Write this line:
[self.view layoutIfNeeded];
I made a small test changing H and V of a small red view:
self.red_POS_H = NSLayoutConstraint.constraints(withVisualFormat: "H:|-90-[redView]", options: defaultOptions, metrics: nil, views: viewsDictionary)
self.red_POS_V = NSLayoutConstraint.constraints(withVisualFormat: "V:|-30-[redView]", options: defaultOptions, metrics: nil, views: viewsDictionary)
self.view.addConstraints(self.red_POS_V!)
self.view.addConstraints(self.red_POS_H!)
and animated it:
// we hope is only one:
let currV = self.red_POS_V![0]
let currH = self.red_POS_H![0]
UIView.animate(withDuration: 3) {
// Make all constraint changes here
currV.constant = 100
currH.constant = 300
self.view.layoutIfNeeded() // Forces the layout of the subtree animation block and then captures all of the frame changes
}
red view moved correctly, if You comment out single line in animation, it does work, horizontally or vertically.
Small project avalable, if You need it.

How to manually use estimatedSize from FlowLayout

In iOS 8 we got a opportunity to create autosize cells with property flowlayout called estimated Size.
But in my case i want use this opportunity manually.
but can't understand how?
In method of delegate sizeForItemAtIndexPath i can't call dequeueReusableCellWithReuseIdentifier, because i get error
[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array
I can't understand, how i can get size of cell in sizeForItemAtIndexPath before cellForItemAtIndexPath?
How it's doing collectionViewFlowLayout?
Also in iOS 8 we got systemLayoutSizeFittingSize but it's also works on already created cells.
I found maybe a solution
+ (CGSize)sizeForViewFromNib:(NSString *)nibName width:(CGFloat)width userData:(id)userData {
UIView *view = viewFromNib(nibName, nil);
[view configForUserData:userData];
CGSize size = [view systemLayoutSizeFittingSize:CGSizeMake(width, SOME_MIN_SIZE) withHorizontalFittingPriority:UILayoutPriorityRequired verticalFittingPriority:UILayoutPriorityFittingSizeLevel];
return CGSizeMake(width, size.height);
}
Point here it's
UIView *view = viewFromNib(nibName, nil);

Xcode 5, why isn't the image resizing?

Hello I am trying to resize a UIImage, but even though I'm not getting any errors it is not working.
hers the code of .h file
IBOutlet UIImageView *Fish;
heres the code of .m file
Fish.frame = CGRectMake(0, 0, 300, 293);
What am I doing wrong?
Thanks for any help
The image is probably not resizing because you are just resizing the image view. Make sure in your storyboard that you make the image view (Fish), have the move ScaleToFill. I can't do screenshot due to reputation ( sorry :( )
Alternately, if your goal is not to resize the image view but to resize the image it is holding, you can do this:
UIImage *image = Fish.image;
UIImage *image = YourImageView.image;
UIImage *tempImage = nil;
CGSize targetSize = CGSizeMake(80,60);
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectMake(0, 0, 0, 0);
thumbnailRect.origin = CGPointMake(0.0,0.0);
thumbnailRect.size.width = targetSize.width;
thumbnailRect.size.height = targetSize.height;
[image drawInRect:thumbnailRect];
tempImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
YourImageView.image = tempImage;
and you would set thumbnailRect to whatever size you want.
Hope this helps! Please search Nerdy Lime on the app store to find all of my apps! Thanks!
I bet your outlet is not hooked up. In your "viewDidLoad" method, try doing this:
if(Fish)
{
Fish.frame = CGRectMake(0, 0, 300, 293);
} else {
NSLog(#"Fish is null; why did I forget to connect the outlet in my storyboard or xib?");
}
And this isn't the best way to resize your UIImageView. If you're using regular springs & struts, you can grow an outlet by clicking the springs & struts to grow based on the superview's size, e.g.:
And if you're doing AutoLayout, there's a different thing you can do (basically pin your view to all four sides of the superview).
Here is how I do it:
1) select the outlet / object you want to add constraints to (in your case, it'll be the fish image view)
2) see the segmented control at the bottom of the Interface Builder window? Click on the second one and you'll see a popover view open up with a list of possible constraints to add.
3) In my example, I'm adding constraints in my ImageView to always be 10 pixels from each edge of the superview (note the four "10"s and solid red lines meaning I'm adding four constraints).
AutoLayout is a pain to get accustomed to (and I'm still learning it myself), but I suspect that once one gets the hang of it, it'll be a powerful new tool especially as Apple brings in additional iOS screen sizes in the very near future.

autostretch uitextview, scrollview and static cells

I have a uitableview with four static cells
First cell has a uitextview
second and third and regular cells
Fourth cell has uitextview
I need first and fourth cell to grow and shrink based on the content size. Fourth cell is going to have much larger text, so whole page should scroll for that.
Any ideas how to do that? Thanks.
D.
You can use :
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) {
// Your code to find height of the text whic will display in UITextView.
}else{
// your default cell height
}
}
Hope this will help you.
All the best !!!

How to set the height in the code of the UITextView created with Xcode storyboard

I can not set the height while running of the UITextView object in Xcode for iPhone (Version 4.6 (4H127).
I have used MainStoryboard_iPhone.storyboard to create the UITextView.
I have tried this, which makes no effect:
UITextView *textMultiLine1;
[textMultiLine1 setContentSize:CGSizeMake(320, 320)];
Thanks for answering, I am a very beginner in Xcode.
You first need to create an Outlet in you header file:
IBOutlet UITextView *textMultiLine1;
Then you need to connect it in the StoryBoard:
Then you must set it's frame:
textMultiline1.frame = CGRectMake(originX, originY, width, height);
Or, if you want to change only it's height:
float newHeight = 200.0;
CGRect textViewFrame = textMultiline1.frame;
textMultiline1.frame = CGRectMake(textViewFrame.origin.x, textViewFrame.origin.y, textViewFrame.size.width, newHeight);
Setting the content size will only change the inner TextView area, resulting in a longer (or shorter, depending on the values you set) scroll

Resources