Adding view to UIScrollView and toggling scrolling/interacting - iphone

I have a UIScrollView in my project. I have a view controller I would like to add as a child of the UIScrollview. Would I just do that like this:
[scrollView addSubview:theViewController.view];
or is there a better way?
(theView is a view, not the TV show)
Furthermore, I would like to be able to use a UIButton in scrollView's parent view controller to toggle whether or not the user is scrolling with scrollView and NOT interacting with theView or NOT scrolling with scrollview and interacting with theView. Should I just have that set the property:
scrollView.userInteractionEnabled = NO;
or would that disable interaction with theView because it's a child?
Thanks for your help!

This is the proper way.
On the scrollView, setScrollEnabled: to turn off scrolling.

Related

UIScrollView as a subview does not scroll

When I set my UIScrollView to be the main view within Interface Builder, the scrolling works as expected. However, when I add the UIScrollView as a subview of UIView the scroll does not work.
So it seems my trouble begins when the UIScrollView is added as a subview to UIView.
The "User Interaction Enabled" it true for both view.
I though perhaps I might have to do something with in touchesBegan something lake passing the touches to the UIScrollView, but have not had much luck with that.
Has anyone seen this before?
scrollView.delegate = scrollView; //or nil may work
It's always good to show a complete working code snippet:
// in viewDidLoad:
//UIScrollview *myScrollView;
UIView_descendent *contentView;
// scrollview won't scroll unless content size explicitly set
[myScrollView setContentSize:contentView.frame.size];
I have not found a way to set contentSize in IB.

Customizing UIScrollView's scrollbars

Okay, so the subject talks for itself - I need to change default scrollbar with my custom image. I've been looking for a solution that doesn't require you to write your own ScrollView class or use hacks like creating an UIView with scrollbar image and repositioning it as you scroll.
One solution I liked was to use a simple UIScrollView category and to access scrollbars as UIScrollView's subviews: http://leonov.co/2011/04/uiscrollviews-scrollbars-customization/#comment-7909 For some reason, though, this one doesn't work for me. When I create UIScrollView and get its subviews array only views that I manually add to scrollview are shown there. I cannot access scrollbars iterating through subviews array. For example, this code:
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10,10,100,100)];
scrollView.userInteractionEnabled = YES;
scrollView.bounces = NO;
scrollView.showsHorizontalScrollIndicator = YES;
NSLog(#"Subviews count is %d", [[scrollView subviews] count]);
will log "Subviews count is 0". Or, if I add X elements to scrollview, "Subviews count is X". Any ideas?
UIScrollbar scroll views are only created when the view is scrolling. They are removed again when the view stops scrolling. That's probably why you can't find them with your category.
You could move your scrollview subview traversing code into the scrollview delegate's scrollViewDidScroll method, which would mean it gets executed whenever the view is scrolling.
I can't help but feel that this is all a horrible and unneccesary hack though, and you'd be better off hiding the scrollbars and implementing them yourself using the delegate methods to determine when to show and hide your custom scrollbar view and the contentOffset property to determine where to position it.

Having an issue with a scroll view moving in UIViewController

I am having an issue with a scrollview when switching UIViewControllers. When I navigate between UIViewControllers the Scrollview seems to adjust itself. How do I stop the scrollview from moving?
You can use the scrollEnabled property to turn off scrolling when you want to switch controllers. [scrollView setScrollEnabled:NO];

Prevent subview from scrolling in a UIScrollView

I have a UIScrollView subclass with a certain subview I'd like to prevent from scrolling (while all the other subviews scroll as normal).
The closest example to this I can think of is UITableView's "index strip" on the right side (look in the Contacts app to see an example). I am guessing this is a subview of the table (scrollview) but it does not move as the user scrolls.
I can't seem to make my subview stay put! How can I accomplish this?
The trick is to adjust the frame of the "non-scrollable" subview inside -layoutSubviews.
Add the view that you want not to move as a sibling view of the scroll view on top of the scroll view instead of as a subview.
You can set it's property called userInteractionEnabled to NO

Anchor a UIView

I have a UITableViewController inside of a UINavigationController.
I want to have a UIView appear over the top of the table view but not susceptible to the scrolling of the table view.
I.e. if the table view is scrolled, the UIView should remain in the same position relative to the screen, rather than relative to the table view. It should appear anchored in a certain position.
What is the best way to achieve this?
EDIT: To clarify, the view should float transparently over the top of the table view.
Many thanks!
I also wanted to have a floating UIView over my tableView.
So, within my RootViewController (which is a UITableViewController), this worked for me
- (void)viewDidLoad {
/* mylabel is a UILabel set in this class */
[self.mylabel setUserInteractionEnabled:NO];
/* navigationController comes from higher up in the navigation chain */
[self.navigationController.view addSubview:self.mylabel];
}
Similar to what Peter said, create a UIView that will contain both the TableView and the subclassed UIView. Such as:
UIView *view = [[UIView alloc] initWithFrame:frame]; // Define frame as you like
[view addSubview:myTableView]; // This is the reference to your tableView
[view addSubview:myAnchoredView]; // This is the reference to your UIView "floating" subclass
You will also need to turn off user interaction for your floating view. I don't know if this will specifically pass the touches to the underlying UIView's or not though:
[myAnchoredView setUserInteractionEnabled:NO];
If this is blocking touches to your tableView, you may need to pass the reference to your tableView to the anchored view at initialization, then pass the touch events along. You can do this by overriding the touch response methods in UIResponder. (If there is a better way, someone please speak up.)
Do you mean the anchored view should appear transparent over the UITableView, or just above, i.e. anchored view uses top 20% of the available space, table view uses the rest?
In any case, create a UIView containing the anchored view and the table view. If you want the anchored view transparent over the table view, it's a bit tricky, because to scroll the table view, touches have to pass through the anchored view.
Add the surrounding view's view controller to the navigation controller instead of just the tableview.
I investigated how UIScrollView keeps its scrollIndicator above the UIScrollView's content and yet unmoving by examining a UIScrollView in the debugger.
The scrollIndicators are UIImageViews. And you can see they are direct descendants of the UIScrollView itself. You can also see that any scrolled content is also a direct descendent. So how is it that the scroll indicators don't move?
I tried updating the position of my static content constantly in - (void)scrollViewDidScroll:(UIScrollView *)scrollView this, surprisingly, works. I'm not sure if it is how UIScrollView itself does it, but without some private magic, it must be something like this.