UITableView null size inside UIScrollView - objective-c

I had a uitableview inside a uiviewcontroller with autolayout constraints to change its size according to the main view.
Once I embedded everything inside a uiscrollview to manage the keyboard covering part of the tableview, the bottom space to superview constraint stopped working and the tableview height is always 0.
I debugged the view and that specific constraint is disabled.
Any ideas?

Related

UITextView inside UIScrollView with AutoLayout

I am trying to place UITextView inside UIScrollView with AutoLayout with no luck. What I have tried is,
I placed UIScrollView inside the main view in Storyboard
I placed UITextView inside UIScrollView in Storyboard and disabled Scrolling Enabled
I set constraints (leading, trailing, top, bottom) on UIScrollView
I set constraints (top, leading, trailing, height) on UITextView
I created IBOutlet of height constraint of UITextView
I set a text (a lot of text which can cause scrolling) on UITextView in viewDidLoad()
I set a height constraint of UITextView with the code below. I have tried it right after setting text in viewDidLoad() and viewDidLayoutSubviews() with no luck
self.textViewHeightConstraint.constant = [self.textView sizeThatFits:CGSizeMake(self.textView.frame.size.width, FLT_MAX)].height;
UITextView is getting its height, but UIScrollView isn't. Is there anything I've missed?
After a few days of research and getting my hands dirty with UIScrollView + UITextView + Auto Layout, I successfully got a fully working UIScrollView. I want to share my solution just in case someone might stuck on the same situation.
Add UIScrollView inside the main view in Storyboard
Add UIView inside the UIScrollView
Add UITextView inside the UIView (the view added in step 2)
Make sure "Scrolling Enabled" of UITextView is unchecked
Add 4 constraints (leading, trailing, top, bottom) on UIScrollView
Add 4 constraints (leading, trailing, top, bottom) on UIView (the view added in step 2)
Add "Width Equally" constraint on UIView (the view added in step 2) and the main view
Add 5 constraints (leading, trailing, top, bottom, height) on UITextView. After this step you shouldn't get any errors and warnings on constraints.
Add UITextView height constraint IBOutlet on the ViewController. #property (nonatomic, weak) IBOutlet NSLayoutConstraint *textViewHeightConstraint; and connect it in Storyboard
Change the UITextView height constraint programmatically. self.textViewHeightConstraint.constant = [self.textView sizeThatFits:CGSizeMake(self.textView.frame.size.width, CGFLOAT_MAX)].height;
After all of these 10 steps, you'll get fully working UIScrollView with UITextView inside and be happy.
It seems that this question has an answer that has worked for a number of people. However, it should be noted that the documentation states:
Placing a text view inside of a scroll view. Text views handle their own scrolling. You should not embed text view objects in scroll views. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.
Before trying to put a UITextView in a UIScrollView you should consider if this is really necessary. If the text view is part of a complex layout within the scroll view, then note the use of the UIView container in the accepted answer.
See also: Scroll Views Inside Scroll Views
If you noticed that the Auto Layout is still complaining about the height of the UIScrollView-
The problem here is that by default UITextView has checked the “Scrolling Enabled” in the IB.
So find that checkbox and uncheck it.
Sodbileg's solution above works, but you do not need all of his steps. I got my scrollview to scroll with a text view inside by following his steps through step 7. For step 8, I added the 4 constraints but not the height (Don't forget the bottom constraint!!). I did not do the rest.
I am not sure, but it seems that the height of the textview adjusts automatically instead of us having to manually change it.

NSTextView does not scroll when created with NSCollectionView

I have an NSTextView that is on a view that is the view of an NSCollectionViewItem. I give the user the option to hide the NSTextView and instead bring up an NSView object in its place on the same view.
However, when I run the program with the NSView object hidden and overlapping the NSTextView, scrolling in the NSTextView does not work correctly; The vertical scroll bar is there and adjusts its size accordingly to the size of the content in the NSTextView. However, the actual scrolling only works by highlighting the NSTextView's content and dragging upwards or downwards. What's more is that if I recreate the view without it being connected to an NSCollectionViewItem and NSCollectionView, it scrolls fine. This is an issue that happens not just with the custom view I have overlapping the NSTextView, but with any view object (buttons, image wells, etc) that overlaps an NSTextView, even if the view object is set as hidden.
Why is scrolling only possible when highlighting and dragging upwards or downwards if the NSTextView is being created with NSCollectionView and there is a view object overlapping it?
Well, I have found a solution that makes little sense but seems to work fine. I subclassed NSTextView and overrode mouseEntered with this:
-(void) mouseEntered:(NSEvent *)theEvent {
NSScrollView *scrollView=(NSScrollView*)self.superview.superview;
NSScroller *scroller=scrollView.verticalScroller;
[scrollView setVerticalScroller:nil];
[scrollView setVerticalScroller:scroller];
}
If anyone has any idea as to why this issue is happening or a better solution and not just a guess-around, please post it

Extend the edges of a UITableView under NavBar and TabBar in UIViewController in iOS 7 to get translucent effect

If I use a UItableViewController it works fine but I need to use a UItableView inside a UIViewController instead. My question is, do i have to manually program it to add extra space at the top and bottom so the first and last cells won't be partially covered and how would I move the refresh indicator down so that its not covered?
Make sure your table view is added as the first subview of your view controller. This will ensure its scroll insets are setup properly.
Also be sure to set the table view's frame to match the bounds of the view controller's view and be sure to set the autoresizingMask to flexible width and height.

UITableView in a UIScrollView - How to make the view scroll, but not the TableView in itself?

Imagine, there is a UIViewController with a UIScrollView in it. At the top of the view there is an UIImageView, some UILabels and other things. Furthermore, there is a UITableView which content is Dynamic Prototypes. I attach a picture to make it clear:
I haven't got a static amount of cells in the UITableView so it could be scrollable. My problem is the following: the UITableView scrolls in itself but I want to scroll the whole View. What is the best possibility to do that?
Possible solutions I've founded today
1) The first thing is: I create a UITableViewController and declare a header section in which I include all my labels, images etc. programmatically (I would love to use the interface builder for that...)
2) Another solution is to calculate the height of the view. I tried the best to do it like this way - but: without success. If this is the best way to do that: Can anybody give an example?
I would ditch the UIScrollView and just use a UITableView. You can add a UIView object as the tableHeaderView of the UITableView just by dragging it in in Interface Builder. Now since everything is part of the UITableView hierarchy, everything will scroll together as expected.
You could also try setting delaysContentTouches to NO on your scrollView. Depending on your setup, this may make the scroll view respond to the touch first instead of the table view.
From Apples UIScrollView Docs:
delaysContentTouches
A Boolean value that determines whether the scroll view delays the
handling of touch-down gestures.
#property(nonatomic) BOOL delaysContentTouches
Discussion
If the value of this property is YES, the scroll view delays handling
the touch-down gesture until it can determine if scrolling is the
intent. If the value is NO , the scroll view immediately calls
touchesShouldBegin:withEvent:inContentView:. The default
value is YES.
You'll have to (as you've mentioned) add the UIView containing the image and buttons to the actual UITableView. Embedding it in the scroll view will produce the undesired behavior that you're seeing.
I would recommend returning the UIView as the header view for the first section of your table view. You can do this by implementing the UITableViewDelegate method:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
If you maintain an IBOutlet to the view containing your image/labels, you can return it here.
this is same demo i hope its helps you from iphone sorce code library
http://developer.apple.com/library/ios/#samplecode/iPhoneCoreDataRecipes/Introduction/Intro.html
thank you

Scrolling a UITableView inside a UIScrollView

I have a UITableView which is a subview of a UIView, then that UIView is a subview of a UIScrollView. How do I detect the touches that should scroll the UITableView?
The UITableView can get item selection events (a cell in the table is selected/tapped) just fine, except that you have to hold down on the cell before it fires. But I can't get the UITableView to scroll, its always the UIScrollView that reacts to the pan gesture.
Any help is greatly appreciated. Thanks in advance!
EDIT:
Solved, though I asked the wrong question. It does work by default as Roman K pointed out. I think the problem was related to having a part of the UITableView outside the bounds of the UIScrollView (the UITableView went over the bottom bounds of the UIScrollView). Setting it to correctly fit inside the UIScrollView fixed it.
Please, make sure that UIScrollView's properties delaysContentTouches and canCancelContentTouches are set appropriately. They control how UIScrollView instance passes touch information to its subviews. By default delaysContentTouches is set to YES. Also, make sure that, if you extended UIScrollView, touchesShouldBegin:withEvent:inContentView: allow touches in the subview.
Otherwise, UITableView scrolling should work by default in your scenario. If you create a test project with just the view hierarchy as described you will see that it is the case. So, compare the two and see what difference affects the scrolling.