Objective
We all know that the implementation of the status bar (StatusBar) automatically return to the top effect, designed to facilitate the user browsing the interface, click the status bar can quickly return to the top of the interface, so mainly for the can scroll UIScrollView
and its subclasses UITableVIew
and UICollectionView
.
This feature is implemented in the following ways.
1. Apple with its own function
Analysis:
First of all, Apple itself has provided this feature, slide up tabView
, click statusBar
, tableView
will automatically return to the original position. As shown in the following figure, click here statusBar
and the top of the screen will display the first one cell
. Add one to a controller tabView
, then the default click statusBar
is automatically returned to the top.
Since Apple has already provided this function, we can use it directly, why do we have to implement it ourselves?
In fact, in some cases this function is not valid. For example, when there are two or two or more subclasses on a window UIScrollView
. For example, add the above tabView
to one first scrollView
, then add it to the scrollView
controller, and then View
click statusBar
, Tabview not automatically back to the top.
Because the effect is valid, it is scrollsToTop
related to the attribute. To view the official documentation, the following points are noteworthy:
1. By default, scrollsToTop
Yes, and only if the property is yes, the click is statusBar
valid.
2. The effect is to allow statusBar
the nearest ScrollView
automatic back to the top of the
3. At the top of the iphone screen, when there are multiple ScrollView
(or its subclasses), if scrollsToTop= YES
ScrollView
more than one, all ScrollView
will not respond statusBar
to the click.
Summary:
From the above analysis we can draw the conclusion that we must ensure that scrollsToTop == YES
the window ScrollView
(and its subclasses) at the same time there is only one. This is the same as to ensure that the click StatusBar, the only existing ScrollView
can automatically return to the top.
How to ensure that the Apple itself with the function has been working?
Solution: We want to go back to the top of the ScrollView
scrollsToTop =YES
other scrollsToTop = NO
.
Sometimes, in order to meet a certain demand, we scrollView
will add more than one above TabView
, to achieve sliding up and down cell
the display of different content, sliding left and right can switch different tabView
, then click statusBar
is no effect. Because of all scrollView
the scrollsToTop =YES
. To display each TableView
time, the clicks statusBar
are valid and must be made to show all but the top TabView
ScrollView
scrollsToTop =NO
. This needs to be judged by which one is displayed TabView
.
The reference code is as follows:
1. Let the bottom of the scrollView
, scrollsToTop =NO
. The other TableView
is the scrollView
subclass of the class.
2. Traverse judgment
Control ScrollView Scrollstotop Property for
(Nsinteger i = 0; i < Self.childViewControllers.count; i++) {
Uiviewcontroller *CHILDVC = self.childviewcontrollers[i];
If the controller's view is not created, skip if
(!childvc.isviewloaded) continue;
If the controller's view is not scrollview, it skips the
if (![ Childvc.view Iskindofclass:[uiscrollview class]) continue;
If the controller's view is ScrollView
uiscrollview *scrollview = (Uiscrollview *) Childvc.view;
Scrollview.scrollstotop = (i = = index);
}
2. Realize your Own
statusBar
add a cover to the area and listen for a masked click event.
UIView
First we thought of using UIView
it to cover. However, here we UIView
are statusBar
not able to use, UIView
will always be in statusBar
the following, so can not receive the Click event. Because statusBar
it is actually one UIWindow
, and the priority is higher than the following keyWindow
. So, the added UIView
will be statusBar
underneath.
UIWindow
Because of the priority of the relationship, we can use one UIWindow
to cover, set window
the priority of the mask is higher statusBar
. Of course, setting the highest priority ( UIWindowLevelAlert
) is certainly possible. Then Window
add a click event to the mask, and the background color is set to transparent.
Dispatch_after (Dispatch_time (Dispatch_time_now, int64_t) (0.1 * nsec_per_sec)), Dispatch_get_main_queue (),
^{ UIWindow * Coverwindow =[[uiwindow alloc]initwithframe:cgrectmake (0, 0, [UIScreen mainscreen].bounds.size.width, 20)];
Self.coverwindow = Coverwindow;
Coverwindow.hidden = NO;
Coverwindow.backgroundcolor = [Uicolor redcolor];
Coverwindow.windowlevel = Uiwindowlevelalert;
Add gesture
UITapGestureRecognizer *tap = [UITapGestureRecognizer alloc]initwithtarget:self action: @selector ( Coverwindowclick)];
[Self.coverwindow Addgesturerecognizer:tap];
-(void) Coverwindowclick {
[uiview animatewithduration:0.5 animations:^{
self.tableView.contentOffset = Cgpointmake (0, 0);
}];
}
Appdelegate Direct monitoring of statusbar clicks
AppDelegate
Implementing Methods in touchesBegan:
-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) Event {
if ([Touches.anyobject Locationinview:nil].y >) return;
[[Nsnotificationcenter defaultcenter]postnotificationname:@ "click" Object:nil];
}
Receive notification, modify tabView
thecontentOffset
-(void) Coverwindowclick {
[uiview animatewithduration:0.5 animations:^{
self.tableView.contentOffset = Cgpointmake (0, 0);
}];
}
Summarize
The above is the entire content of this article, I hope that the development of iOS can help you, if you have any questions you can exchange messages, thank you for your support cloud habitat community.