I have a Nib with a scrollview which i use on my controller. I have to make some constant calculations based on the scrollview.subviews count. However, surprisingly the scrollview is always starting with two uiimage views.
The scrollview at my Nib file is empty, and i have even checked the nib source code to assure there is no garbage there. I also deleted the scrollview and created another with the same result.
I am always receiving the same views there (always at, so my scrollview.subviews.count always start at 2. What could be causing this??
If I print scrollview subviews just after initializing view..
[[NSBundle mainBundle] loadNibNamed:#"TileScreen" owner:self options:nil];
NSLog(#"Started scrollview with subviews %#", _scrollView.subviews);
I receive:
Started scrollview with subviews (
"<UIImageView: 0x1dcb50; frame = (294 400; 7 7); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x1e51b0>>",
"<UIImageView: 0x1bb6a0; frame = (294 400; 7 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x1a9b50>>"
)
Well before I posted this question I understood the problem. I wanted to share in case someone falls into this.
The problem is that the "Show horizontal scrollbar, Show vertical scrollbar" add the mentioned UIImageViews. You can avoid this by simply unchecking the property on the IB.
If you want to show the scrollbars though, you will need to take into account these two views in your count.
Related
I used this code from Apple's sample app and adjusted it to mine. I want the UIImageView gameOverFalling to run this method (fall and kind of bounce). It should collide with the UIView finalScoreView, as you can see in line 2.
-(void)gameOverFallingMethod{
UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:finalScoreView];
UIGravityBehavior *gravityBeahvior = [[UIGravityBehavior alloc] initWithItems:#[self.gameOverFalling]];
[animator addBehavior:gravityBeahvior];
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:#[self.gameOverFalling]];
// Apple said: "Creates collision boundaries from the bounds of the dynamic animator's
// reference view (self.view)."
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
collisionBehavior.collisionDelegate = self;
[animator addBehavior:collisionBehavior];
self.animator = animator;
}
However when I run my app, it returns a Thread 1: SIGABRT error. In the console:
View item (<UIImageView: 0x10aaa0c70; frame = (15 175; 288 42);
autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x10aa7e0a0>>)
should be a descendant of reference view in <UIDynamicAnimator: 0x10ae17610>
Stopped (0.000000s) in <UIView: 0x10aa9e1e0> {{0, 0}, {288, 195}}
It works when I replace 'finalScoreView' on line 2 with 'self.view', but then it falls to the bottom of the whole screen.
Any ideas what I did wrong? I would love to know, thanks!
It appears from the exception that self.gameOverFalling does not descend from finalScoreView in your view hierarchy. See the quote from UIDynamicAnimator class reference:
To animate views, create an animator with the initWithReferenceView:
method. The coordinate system of the reference view serves as the
coordinate system for the animator’s behaviors and items. Each dynamic
item you associate with this sort of animator must be a UIView object
and must descend from the reference view.
I solved this by adding to subview before adding any behaviour.
addSubview(square)
var gravity = UIGravityBehavior(items: [square])
gravity.magnitude = gravitySpeed
animator.addBehavior(gravity)
You probably forgot to add your items to the reference view using:
finalScoreView.addSubview(self.gameOverFalling)
I'm targeting iOS7 in my latest app, and tapping on the status bar doesn't seem to scroll a tableView or collectionView to the top.
I've set self.tableView.scrollsToTop = true and still nothing happens.
I know Apple significantly changed the status bar in iOS7, but did those changes break the scrollsToTop functionality?
Update
In response to a comment in one of the answers, I tested to ensure that my collection view was indeed the only scrollView on the screen, and it was:
(lldb) po [self.view recursiveDescription]
<UIView: 0x1092ddf0; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x109357e0>>
| <UICollectionView: 0x11351800; frame = (0 0; 320 568); clipsToBounds = YES; opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0x10966080>; layer = <CALayer: 0x109623a0>; contentOffset: {0, -64}> collection view layout: <UICollectionViewFlowLayout: 0x10940a70>
| | <UIImageView: 0x10965fa0; frame = (0 564.5; 320 3.5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x10965ee0>> - (null)
| | <UIImageView: 0x10948f60; frame = (316.5 561; 3.5 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x10966030>> - (null)
Update #2
Not sure if it matters, but I'm using a standard iOS7 NavigationController where the navigationBar is transparent and applies a blur to my collection/tableViews as they scroll underneath.
Update #3
Figured it out. Turns out I did have more than one scrollView on the screen. My app has a left drawer menu underneath the main part of the app, and that menu has a tableView for the options. I simply set self.menuTable.scrollsToTop = false and everything worked as expected throughout the rest of the app. Didn't have to implement the scrollView Delegate methods or anything.
Do you have more than one scroll view/table view/collection view on screen? If so, only one of them can have scrollsToTop set to YES, otherwise iOS7 will not scroll any of them to the top.
You can also implement the UIScrollViewDelegate method scrollViewShouldScrollToTop: and return YES if the passed in scroll view is equal to the one that you want to scroll to the top:
- (BOOL) scrollViewShouldScrollToTop:(UIScrollView*) scrollView {
if (scrollView == self.myTableView) {
return YES;
} else {
return NO;
}
}
The short answer is there's nothing different in iOS7. As long as there isn't more than one UIScrollView loaded, your tableView or collectionView will scroll to the top when the user taps the status bar. The key here is loaded; another scrollView doesn't necessarily have to be on screen to conflict with another scrollView that is.
Sliding drawers in the left/right are very popular these days, and this was the reason for my problem. I have a menu containing my navigation options, and these are all held by a UITableView. I had to make sure that I set menuTable.scrollsToTop = false before I could get things working in the other parts of my app.
My problem was that I had a UITextView with scrollsToTop set to YES, so my UITableView wasn't responding to the gesture. In short, make check all other scrollable views.
for others :
Remember that the scroll view you are searching can also be a UIWebView..not just UITableView.
Another important thing is that it's not only about VISIBLE scrollViews, but LOADED scrollviews.
If you don't find the scrollView, you can always
insert UITableView test table, immediately when app is starting, check if it's scroll to top,
and then load more and more views, until the test table stop scrolling to top.
If your table cells are dynamic, remove the following:
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
return YES;
}
Create a new function as follows:
- (void) disableScrollsToTopPropertyOnAllSubviewsOf:(UIView *)view {
for (UIView *subview in view.subviews) {
if ([subview isKindOfClass:[UIScrollView class]]) {
((UIScrollView *)subview).scrollsToTop = NO;
}
[self disableScrollsToTopPropertyOnAllSubviewsOf:subview];
}
}
Call the function above in - (void)viewDidLoad
[self disableScrollsToTopPropertyOnAllSubviewsOf:self.view];
Now enable ScrollsToTop for your table view as follows:
[myTableView setScrollsToTop:YES];
This always works for me:
[self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
When I create a label in Interface Builder and hook the outlet to a viewController, and log the label in viewDidLoad as follows:
NSLog(#"label: %#",self.label);
It gives me a frame of (0,0,0,0). self.view gives me a size over 0. An older program's label also gives me a size over 0. It will log the text in the label, so it is hooked up right. Is this a change in Xcode 4.5? How do I access the frames that I set in Interface Builder?
this is the log:
UILabel: 0x754e1b0; frame = (0 0; 0 0); text = 'this is a label'; clipsToBounds = YES; opaque = NO; autoresize = TM+BM; userInteractionEnabled = NO; layer =...
Usually the frame layouts are unreliable in viewDidLoad. In the case of a nib this shouldn't be a problem. Are you using AutoLayout by any chance ?
Please also log the same values in viewWillAppear, this will certainly have the correct values. I usually use viewWillLoad to initiate my UI elements, and viewWillLoad to place them correctly.
Try this:
NSLog(#"Label Frame: %#",NSStringFromCGRect(self.label.frame));
Very strange behavior, there is a round dot in the center of the screen using this code, and a UIScrollview with nothing inside in a nib. I expect that UIScrollview should be empty. The dot blurs and disappears when I scroll the screen.
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *subviews = [closetScroll subviews];
UIImageView *strange=[subviews objectAtIndex:0];
strange.center = CGPointMake([[UIScreen mainScreen] bounds].size.width/2, [[UIScreen mainScreen] bounds].size.height/2);
strange.alpha=1;
NSLog(#"%#",subviews);
}
The console output is:
<UIImageView: 0x4b1f780; frame = (380.5 508.5; 7 7); opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x4b1f820>
Does anyone know why?
After magnifying the UIImageView and tweaking with configurations, I have come to conclude that the UIImageView is actually the scroll bar, and if horizontal and vertical scroll is enabled, an "empty" UIScrollview has two subviews inside.
I had the same issue. Calculating the number of subviews can be very deceptive, because of this "feature".
Start the application and count the number of subviews, this will be 1. Now, use you mouse in the simulator or finger on the device and swipe from the right to the left. Count the number of subviews. The number will be 2.
I can deduce nothing else than that this extra UIImageView is produced by Cocoa Touch to render the background in the right color when "bouncing" beyond the end of the UIScrollView's bounds.
so I followed this guide ("The Technique for Static Row Content") to create my own custom UITableViewCell-s that would contain one image.
The following code is excerpt from my tableView:cellForRowAtIndexPath: method:
UIImageView *imageView = (UIImageView *)[imageViewCell viewWithTag:1];
imageView.image = [UIImage imageNamed:imageName];
cell = imageViewCell;
NSLog(#"%#", cell);
...
return cell;
imageViewCell refers to my custom cell created in interface builder. As you can see I'm trying to change image each time.
Everything works fine, but if I use reloadSections:withRowAnimation: on the UITableView, this cell disappears.
Here's console output:
<UITableViewCell: 0x9c68fe0; frame = (0 0; 302 215); autoresize = RM+BM; layer = <CALayer: 0x4b88110>>
<UITableViewCell: 0x9c68fe0; frame = (0 120; 320 102); autoresize = W; layer = <CALayer: 0x4b88110>>
<UITableViewCell: 0x9c68fe0; frame = (-320 120; 320 102); alpha = 0; autoresize = W; layer = <CALayer: 0x4b88110>>
<UITableViewCell: 0x9c68fe0; frame = (-320 121; 320 105); alpha = 0; autoresize = W; layer = <CALayer: 0x4b88110>>
<UITableViewCell: 0x9c68fe0; frame = (-320 120; 320 111); alpha = 0; autoresize = W; layer = <CALayer: 0x4b88110>>
So as you can see it's frame and alpha is changed to weird values and stays like that.
That makes sense, because I'm not initializing it each time again, it's initialized only once after waking up from nib.
How do I reset its attributes to make it visible again? I found method called prepareForReuse, but that didn't work. I need something that would reset alpha and frame to make it appear again.
Solution with loading nib each time
To be more clear about my first approach: I created the table view cell in the nib of view controller. I set up an outlet, so I could use it in tableView:cellForRowAtIndexPath:.
Since the cell's attributes were messed up after animation I figured that recreating that cell would definitely help. The problem was my nib was loaded only once and I (still) don't know how to do something like reinitialization on a view that was initialized by nib file.
So I decided to create a new nib file and load it each time. It's not exactly what I was looking for, but it works. Here's what the code looks like, it's very simple:
[[NSBundle mainBundle] loadNibNamed:#"TestTableCell" owner:self options:nil];
UIImageView *imageView = (UIImageView *)[imageViewCell viewWithTag:1];
imageView.image = [UIImage imageNamed:imageName];
cell = imageViewCell;
imageViewCell = nil; // imageViewCell is still an outlet and setting
// it to nil makes the nib load it again the next
// time - so I'm sure I'll get a new instance.
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Using reloadSections:withRowAnimation: with UITableViewRowAnimationNone actually solves the problem for me, and it still shows animation on adding/deleting cells from section.
I still believe, that this is a bug in UIKit.
The cell is disappearing because something is changing its frame such that it eventually gets drawn off screen.
If the size of the image changes, the image view itself is set to autoresize and the tableview cell's content view does as well, that might cause the cell's frames to migrate depending on the order of which the images load.