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);
Related
I want the screen only shows one tableveiwCell with headerView.I added a UIView as header view on the top of UITableView with "Size classes" in storyboard(Just drag a UIView on the top of the UITableView), it can compatible with all devices screen size in this way.
So change header view's height by constraints is what i want to. but when i try to do that, i cant set constraints for the headerView(Xocde doesn't enable me select any constrains in storyboard, Image 2 below). As below.
Any ideas, thanks!
The project code is here: https://github.com/williamhqs/AutoLayoutTableViewHeaderView
EIDT:
Seems it still can't be set in storyboard.
Then i will have to change table header view's frame by code then update constraints.
UIView *v = self.tableView.tableHeaderView;
CGRect fr = v.frame;
fr.size.height = [UIScreen mainScreen].bounds.size.height -100;
v.frame = fr;
[self.tableView updateConstraintsIfNeeded];
Then i will have to change table header view's frame by code then update constraints.
UIView *v = self.tableView.tableHeaderView;
CGRect fr = v.frame;
fr.size.height = [UIScreen mainScreen].bounds.size.height -100;
v.frame = fr;
[self.tableView updateConstraintsIfNeeded];
i think that UIView doesn't have any superview So that it is not showing the constraints for view.
I have a UIView inside a UIScrollView. I can easily pin the height to the UIScrollView's frame height.
How do I add a constraint that pins to the UIScrollView's contentSize instead?
Thanks!
UIScrollView have dynamic constraints, left, top, width and height are generated at runtime. If you put a UIView inside a UIScrollview and Pin fixed constraints in Interface Builder it will generate an error because the parameters are relative to Superview/Container View.
You can try some workarounds:
1- Add UIView constraints Programmatically
http://www.thinkandbuild.it/learn-to-love-auto-layout-programmatically/
2- Manually resize your view bounds in initWithFrame function inside a UIView Subclass
Please give me any feedback about your progress.
The answer "Adding constraints programatically" is correct but it was a little light on detail for me to accept it as the full answer.
Here's how I did it!
Remove all storyboard constraints on the WebView
Add a Placeholder constraint in storyboard for the constraint that you will add with code. This step is very important (and easily missed) or you will get an error about conflicting constraints.
Add code to webviewDidFinishLoad delegate method
--Code--
- (void)webViewDidFinishLoad:(UIWebView *)webView {
_scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, _headerImageView.frame.size.height + webView.scrollView.contentSize.height);
_webView.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *viewsDictionary = #{#"myWebView":_webView};
NSString *constraintsString = [NSString stringWithFormat:#"V:[myWebView(%i)]", (int)_scrollView.contentSize.height];
NSArray *constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:constraintsString options:0 metrics:nil views:viewsDictionary];
[_webView addConstraints:constraint_H];
}
I have a subview loaded into an UIView. In the subview's .m file I have the following:
- (void)startAnimation {
// Array to hold png images
imageArray = [[NSMutableArray alloc] initWithCapacity:22];
animatedImages = [[UIImageView alloc] initWithImage:viewForImage];
// Build array of images, cycling through image names
for (int i = 1; i < 22; i++){
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"image%d.png", i]]];
}
animatedImages.animationImages = [NSArray arrayWithArray:imageArray];
// One cycle through all the images takes 1 seconds
animatedImages.animationDuration = 2.0;
// Repeat forever
animatedImages.animationRepeatCount = 0;
// Add subview and make window visible
[viewForMovie addSubview:animatedImages];
// Start it up
animatedImages.startAnimating;
NSLog(#"Executed");
}
Please be noted that I have in the .h file:
UIImageView *animatedImages;
NSMutableArray *imageArray;
UIView *viewForMovie;
#property(nonatomic,retain)IBOutlet UIView *viewForMovie;
and in the .m file:
#synthesize viewForMovie;
and I have connected viewForMovie to a UIView in IB. I've been on this for several hours now and have tried many variations I've found on the web but cannot get it to work. There are no errors and the other GUI graphics in the subview appear very nicely....but the animation just doesn't appear over top where it should. Also the NSlog reports that the method has in fact been called from the parent. Can anyone see any blaring issues? Thx.
PS: I'm pretty new at this.
Based on the code shown and the behavior you see so far, here are my suggestions:
Make sure the viewForMovie IBOutlet is connected properly in Interface Builder. If it's not connected properly (and so nil), nothing will appear. If you didn't mean to make it an IBOutlet in the first place, then you'll need to manually create it and add it as a subview to self before using it.
Not sure why you have the viewForMovie UIView in the first place. Is this subview's class (let's call it MySubview) a subclass of UIView? You can just show the animation in self instead of adding another subview inside it. Are you going to add more uiviews to this subview besides the viewForMovie?
To get rid of the "may not respond to" warning, declare the startAnimation method in the MySubview.h file (under the #property line):
-(void)startAnimation;
The fact that the warning says "UIView may not respond" also tells you that the parent view has declared newView as a UIView instead of MySubview (or whatever you've named the subview class). Change the declaration in the parent from UIView *newView; to MySubview *newView;.
In the initWithImage, what is "viewForImage"? Is it a UIImage variable or something else?
If all of the images are the same size and fit in the subview as-is, you don't need to set the frame--the initWithImage will automatically size the UIImageView using the init-image dimensions.
Double check that the images you are referencing in the for-loop are named exactly as they are in the code and that they have actually been added to the project.
Finally, you should release the objects you alloc in startAnimation. At the end of the method, add:
[imageArray release];
[animatedImages release];
The only item, however, that I think is actually preventing the animation from appearing right now is item 1.
I am doing manual layouting for my Cocoa application and at some point I need to figure out what the inner size of a NSView subclass is. (E.g. What is the height available for my child view inside of a NSBox?)
One of the reasons is that I am using a coordinate system with origin at the top-left and need to perform coordinate transformations.
I could not figure out a way to get this size so far and would be glad if somebody can give me a hint.
Another very interesting property I would like to know is the minimum size of a view.
-bounds is the one you're looking for in most views. NSBox is a bit of a special case, however, since you want to look at the bounds of the box's content view, not the bounds of the box view itself (the box view includes the title, edges, etc.). Also, the bounds rect is always the real size of the box, while the frame rect can be modified relative to the bounds to apply transformations to the view's contents (such as squashing a 200x200 image into a 200x100 frame).
So, for most views you just use [parentView bounds], and for NSBox you'll use [[theBox contentView] bounds], and you'll use [[theBox contentView] addSubview: myView] rather than [parentView addSubview: myView] to add your content.
Unfortunately, there is no standard way to do this for all NSView subclasses. In your specific example, the position and size of a child view within an NSBox can be computed as follows:
NSRect availableRect = [someNSBox bounds];
NSSize boxMargins = [someBox contentViewMargins];
availableRect = NSInsetRect(availableRect, boxMargins.width, boxMargins.height);
If you find yourself using this often, you could create a category on NSBox as follows:
// MyNSBoxCategories.h
#interface NSBox (MyCategories)
- (NSRect)contentFrame;
#end
// MyNSBoxCategories.m
#implementation NSBox (MyCategories)
- (NSRect)contentFrame
{
NSRect frameRect = [self bounds];
NSSize margins = [self contentViewMargins];
return NSInsetRect(frameRect, margins.width, margins.height);
}
#end
And you would use it like so:
#import "MyNSBoxCategories.h"
//...
NSRect frameRect = [someNSBox contentFrame];
[myContentView setFrame:frameRect];
[someNSBox addSubview:myContentView];
The bounds property of NSView returns an NSRect with the origin (usually (0,0)) and the size of an NSView. See this Apple Developer documentation page.
I'm not sure (I never had to go too deep in that stuff), but isn't it [NSView bounds]?
http://www.cocoadev.com/index.pl?DifferenceBetweenFrameAndBounds
I have a custom NSTableView subclass which is bound to a data source (an NSArray) which updates asynchronously. When items are added to the array, rows are automatically added to the tableview. Awesome!
My question is this: How can I detect that this magic has happened so that I can perform some other tasks related to the display of my custom tableview? Is there a method that I can override in my subclass which will be called when the tableview is updated?
You don't need to subclass NSTableView to change its height based on the number of rows. In your controller, just monitor the data array using KVO and adjust the frame size of the tableview's scrollview (you can find it using enclosingScrollView) when rows are added or removed. I've used this technique before and it's worked well. You can use the tableview's rowHeight and intercellSpacing methods to calculate the height of the frame.
Looked high and low for days on this solution. It worked like a charm, thanks! Here's a sample of my code for others to follow:
// tv = NSTableView
// view = NSView
int height = ([tv rowHeight] + [tv intercellSpacing].height) * [itemNodes count];
NSScrollView *sv = [tv enclosingScrollView];
NSRect svFrame = [sv frame];
svFrame.size.height = height;
[sv setFrame:svFrame];
NSRect viewFrame = [view frame];
viewFrame.size.height = height;
[view setFrame:viewFrame];