Scroll to top with status bar tap - ios

I've looked through various SO questions on the topic and I have not found a solution. I have a UIViewController with a UITableView and a UICollectionView. I want the UICollectionView to scroll to the top, when the user taps it.
The documents say if you have more than one UiScrollView subclass - you need to set them to no and the UiScrollView you want to scroll to the top, to yes.
So I wrote this bit of code to go through all my views:
for (UIScrollView *view in self.view.subviews) {
if ([view isKindOfClass:[UIScrollView class]]) {
view.scrollsToTop = NO;
}
}
self.collectionView.scrollsToTop = YES;
This way I am sure any subclass of UiScrollView has it's scrollsToTop property set to no.
However tapping on the status bar does not do anything.
Can someone tell me what I am missing here?
Thank you

It seems that you are only iterating through the subviews of your main view. Your UITableView may be nested inside another view. Try doing the following;
//in view did load
[self setScrollToTopFalse:self.view];
self.collectionView.scrollsToTop = YES;
-(void)setScrollToTopFalse:(UIView *)v
{
for (UIView * v1 in [v subviews]) {
if ([[v1 class]isSubclassOfClass:[UIScrollView class]]) {
((UIScrollView *)v1).scrollsToTop = NO;
}
[self setScrollToTopFalse:v1];
}
}

Related

How to get subviews of UIButtonBarStackView?

I am trying to get the subviews of UIButtonBarStackView, so that I can find the right bar button and disable it. I am trying to hide share button in QLPreviewController with usdz files. I have subclassed QLPreviewController and I have this code in viewDidAppear. I can get the subviews till this point and these subviews of StackView returns empty array. Here is my code -
UINavigationController *navController = self.childViewControllers.firstObject;
for(UIView *toolbar in navController.view.subviews[2].subviews) {
if ([NSStringFromClass([toolbar class]) containsString:#"ContentView"]){
for(UIView * stackView in toolbar.subviews) {
if ([stackView isKindOfClass:[UIStackView class]]) {
//StackView subview is empty.
}
}
}
}
Any help is much appreciated. Thanks!

ScrollsToTop feature is not working in iOS9 properly

I have a UIViewController having a UITableView with large data. I want to implement the feature of scrolltotop on press of the statusbar.
I have a lot of UIView set through my storyboard so I first try to set scrollsToTop = NO for all and then set the scrollsToTop = YES for the UITableView specifically in viewDidLoad.
The same settings/configuration works with iOS8 but not with iOS9.
I have applied below code to disable scroll for all subviews in start:
- (void)disableScrollsToTopPropertyOnAllSubviewsOf:(UIView *)view {
for (UIView *subview in view.subviews) {
if ([subview isKindOfClass:[UIScrollView class]]) {
((UIScrollView *)subview).scrollsToTop = NO;
}
[self disableScrollsToTopPropertyOnAllSubviewsOf:subview];
}
}
Try it using content offset
self.tableView.contentOffset = CGPointMake(0, 0 - self.tableView.contentInset.top);
Make sure you call it on viewDidAppear:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// Call scroll to top here
}
I hope this can help.
Fortunately, this started working flawlessly in iOS 10.0
You might have more than one UIScrollView in your view hierarchy, and you have to set only the one you want to scroll to top to Yes and the others scrollsToTop = No;.

UIPageViewController - Don't allow to move if no pages

In my app I do not want it to be possible to drag if there is no pages before and/or after.
See the images below.
I do not want it to be able to move if there is no pages before or after the current page. No matter how much you drag, I want it to stay still like the second image, if there is no pages before and/or after.
How it is now when dragging:
How I want it to be when no pages before and/or after when dragging:
Is this what you're looking for?
Disable bounce effect in UIPageViewController:
for (UIView *view in self.pageViewController.view.subviews ) {
if ([view isKindOfClass:[UIScrollView class]]) {
UIScrollView *scroll = (UIScrollView *)view;
scroll.bounces = NO;
}
}

Adding UISearchBar with Scope button as subview to UIView

I want to add UISearchbar with two scoop button to my UIView. i do not want to either navigation or table view.
when user open model view page from sheet comes up with Search option and rest. when user click on Search button scoop button to be visible.
i tried to do but i am not getting desire result.
how to remove Background of scope button, i have removed uisearchbar, similar way i do not want white background for button. is it possible. showsScopeBar i am making FALSE still background can be visible.
To remove Background image of scope buttons just try :
Class segmentedControlBackgroundView = NSClassFromString(#"_UISegmentedControlBackgroundView");
for(UIView *view in self.searchBar.subviews) {
if([view isKindOfClass:[UISegmentedControl class]]){
for(UIView *subview in view.subviews) {
if ([subview isKindOfClass:segmentedControlBackgroundView]) {
subview.hidden = YES;
}
}
}
}

iOS: setting Exclusive Touch to all buttons in a view

My app has many buttons in a Window and I want to set Exclusive Touch all of them together. Do you have any suggestion about this? Thanks
There is a way to set exclusive touch to all buttons in your app, may be helpful.
#import </usr/include/objc/objc-class.h>
static IMP gOringinalWillMoveToSuperview = nil;
static id newMoveToSuperviewPlusSettingExclusiveTouch(id self,SEL selector,...)
{
va_list arg_list;
va_start( arg_list,selector);
gOringinalWillMoveToSuperview(self,selector,arg_list);
[self setExclusiveTouch:YES];
return nil;
}
-(void)addSettingExclusiveTouchToAllUIViewMethodWillMoveToSuperview
{
gOringinalWillMoveToSuperview = class_getMethodImplementation([UIButton class], #selector(willMoveToSuperview:));
class_replaceMethod([UIButton class], #selector(willMoveToSuperview:), &newMoveToSuperviewPlusSettingExclusiveTouch, "v#:");
}
if you don't understand this, you can refer to this and this.
Are you just looking for an easy way to set them all at once?
If you have all the buttons in an array (e.g. they're all connected to the same IBOutletCollection) you can use key value coding to set the exclusiveTouch property of the array:
[buttonArray setValue:[NSNumber numberWithBool:YES] forKey:#"exclusiveTouch"];
NSArray will then invoke the same method on every item in the array.
-(void)setExclusiveTouchForButtons:(UIView *)myView
{
for (UIView * v in [myView subviews]) {
if([v isKindOfClass:[UIButton class]])
[((UIButton *)v) setExclusiveTouch:YES];
else if ([v isKindOfClass:[UIView class]]){
[self setExclusiveTouchForButtons:v];
}
}
}
then call this function at viewDidAppear
If these buttons are all in the same view, you can loop through the view's subviews, test for whether the particular subview is a button (or test for a tag if you have one set) and set exclusiveTouch on each.
I just found an answer for this:
#pragma mark Set Buttons Exclusive Touch Yes
-(void)setExclusiveTouchForButtons:(UIView *)myView
{
for (UIView * button in [myView subviews]) {
if([button isKindOfClass:[UIButton class]])
[((UIButton *)button) setExclusiveTouch:YES];
}
}
Source
If you want to set exclusiveTouch for ALL UIButtons in your whole application method swizzling will be the perfect solution for you.
This answer explains the way very well : https://stackoverflow.com/a/24534814/976246 , and it works perfectly for me.
Also go through this article to know how this (http://nshipster.com/method-swizzling/) tecknique can be used for various purposes.
If you are adding buttons pragmatically, then send a message to the button [button setExclusiveTouch:YES]; for each buttons before adding to its super view. Else if you are using xib, you have to send the same message to the button in viewDidLoad or in loadView.
Here is some code in swift that will set exclusive touch to all buttons in your viewcontroller's view
for button in self.view.subviews {
if(button.isKindOfClass(UIButton)){
(button as! UIButton).exclusiveTouch = true
}
}
Even though the question was asked many years ago, here is a simple way to set isExclusiveTouch for all subviews that are buttons. This example shows code for subviews of self.view. Change self.view to your view of interest.
Seems like cleaner code for anyone with questions on this in the present.
for subview in self.view.subviews
{
if subview is UIButton {
subview.isExclusiveTouch = true
}
}

Resources