I have a Controller of which I have a few subviews. At a certain point in the code, I remove them, and then later on, I add them. Here is the code I use to add them:
[self.view addSubview:dateLabel];
[self.view addSubview:tableView];
Right after these lines, I print out:
NSLog(#"%#",[self.view subviews]);
and the output I get is:
"<UINavigationBar: 0x614a8f0; frame = (0 0; 320 48); layer = <CALayer: 0x6129ee0>>",
"<UILabel: 0x614d500; frame = (0 48; 320 25); text = 'Dec. 15, 2010 - Jan. 15, ...'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x614d570>>",
"<UITableView: 0x507ee00; frame = (0 73; 320 390); clipsToBounds = YES; layer = <CALayer: 0x9f57930>; contentOffset: {0, 0}>"
However, the only thing I see on my screen is the navigation bar. Why is the label and the tableView not showing up even if it says here that it is a subView?
I have also tried to add the following code after adding the subviews:
[self.view bringSubviewToFront:tableView];
[self.view bringSubviewToFront:dateLabel];
but it still does not work.
Is your controller a UIViewController? Have you checked it has been presented correctly? For example with presentModalViewController:self ?
Related
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];
Problem
I can't seem to adopt Auto Layout into my existing project.
Details
I was having the same issue before as this question presentViewController: crash on iOS <6 (AutoLayout) but none of the provided answers were a solution for me: I'm using all storyboard views with no xibs. My 'Use Auto Layout' setting is already turned off and I am using nothing but iOS 6.
My view controller was initially crashing, so I set the constraints to be added with a delay and now my app crashes during any addConstraints: call. Worst part is that it won't tell me why.
Code
I will link my code, but its pretty straight forward.
-(void)addAllConstraints
{
NSDictionary * views = NSDictionaryOfVariableBindings(_memoryImage, _peopleView, _contentHolder, _commentsTableView);
NSArray * constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:[_memoryImage]-50-[_peopleView]-0-[_contentHolder]-0-[_commentsTableView]" options:0 metrics:nil views:views];
NSLog(#"Views %#, Constraints %#", views, constraints);
[_peopleView addConstraints:constraints];
[_memoryImage addConstraints:constraints];
[_contentHolder addConstraints:constraints];
[_commentsTableView addConstraints:constraints];
}
App crashes on _peopleView's call to addConstraints. Both the views and the NSLayoutConstraints are successfully created.
Any ideas? Thank you, Happy Holidays.
EDIT:
Adding Crash logs to show that nothing useful is showing:
2012-12-25 10:40:13.936 -----[4955:907] Views {
"_commentsTableView" = "<UITableView: 0x1eb6be00; frame = (0 372; 320 100); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x1e51ce00>; layer = <CALayer: 0x1e51cee0>; contentOffset: {0, 0}>";
"_contentHolder" = "<UIView: 0x1e5c6590; frame = (0 270; 320 112); layer = <CALayer: 0x1e5c27f0>>";
"_memoryImage" = "<UIButton: 0x1e5c4aa0; frame = (0 0; 320 280); opaque = NO; layer = <CALayer: 0x1e5c4b60>>";
"_peopleView" = "<UIView: 0x1f0ceea0; frame = (0 230; 320 50); layer = <CALayer: 0x1f0cf790>>";
Constraints (
"NSLayoutConstraint:0x1e51a880 V:[UIButton:0x1e5c4aa0]-(50)-[UIView:0x1f0ceea0]",
"NSLayoutConstraint:0x1e5ba4e0 V:[UIView:0x1f0ceea0]-(0)-[UIView:0x1e5c6590]",
"NSLayoutConstraint:0x1e51b860 V:[UIView:0x1e5c6590]-(0)-[UITableView:0x1eb6be00]"
)
}
(lldb)
Constraints are supposed to be added to the view that is the superview of the subviews. So, if these objects are in your main view, then you should have (and none of the other addConstraints: lines):
[self.view addConstraints:constraints];
Also, your dictionary, views, should be nil terminated (I don't know whether this is necessary or not. I've noticed in an Apple example that they didn't do this, but the function definition shows it with the nil).
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.
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.
i am setting the size of a UIScrollView in viewDidLoad: but when I try to get the height of it, i am getting 0 in the console
here is my code:
- (void)viewDidLoad {
[scrollView setContentSize:CGSizeMake(320,500)];
NSLog(#"scrollView: %#", scrollView);
NSLog(#"scrollView.contentSize.height: %i", scrollView.contentSize.height);
[super viewDidLoad];
}
and in the console log i get
scrollView: <UIScrollView: 0x4974110; frame = (0 0; 320 367); clipsToBounds = YES; autoresize = RM+TM; layer = <CALayer: 0x4974010>>
scrollView.contentSize.height: 0
Shouldn't scrollView.contentSize.height be returning 367? On a related note, i specified the height to be 500 via [scrollView setContentSize:], but that doesn't appear to be applied either?
No. The contentSize is the size of the stuff inside the scrollview. Since nothing is inside the scrollview (ie you're not scrolling anything), the height is 0. The only way to get a valid contentSize is after you've added a subview.