Skip to content

Instantly share code, notes, and snippets.

@Sunnyztj
Last active August 29, 2015 13:56
Show Gist options
  • Save Sunnyztj/8927438 to your computer and use it in GitHub Desktop.
Save Sunnyztj/8927438 to your computer and use it in GitHub Desktop.
navigation bar and search bar hide_show code
mbcoll
- (id)viewController
{
UIResponder *nextResponder =[[self superview] nextResponder];
if ([nextResponder isKindOfClass:[StoreListViewController class]]) {
return (StoreListViewController*)nextResponder;
}
else if ([nextResponder isKindOfClass:[OfferListViewController class]]){
return (OfferListViewController*)nextResponder;
}
return nil;
}
- (CGFloat)getListViewTop
{
UIViewController* uvc = [self viewController];
CGFloat searchBarOriginY = 0.0;
CGFloat searchBarSizeHeight = 0.0;
if ([uvc isKindOfClass:[StoreListViewController class]]) {
searchBarOriginY = ((StoreListViewController*)uvc).storeSearchB.frame.origin.y;
searchBarSizeHeight = ((StoreListViewController*)uvc).storeSearchB.frame.size.height;
} else if ([uvc isKindOfClass:[OfferListViewController class]]) {
searchBarOriginY = ((OfferListViewController*)uvc).filterSearchB.frame.origin.y;
searchBarSizeHeight = ((OfferListViewController*)uvc).filterSearchB.frame.size.height;
}
// NSLog(@"Y : %2f, H : %2f", searchBarOriginY, searchBarSizeHeight);
if (searchBarSizeHeight + searchBarOriginY >= 20.0) {
return (searchBarOriginY + searchBarSizeHeight) > 152.0 ? 152 : (searchBarOriginY + searchBarSizeHeight);
}
return 20.0;
}
- (BOOL)isListViewMove
{
UIViewController* uvc = [self viewController];
CGFloat searchBarOriginY = 0.0;
if ([uvc isKindOfClass:[StoreListViewController class]]) {
searchBarOriginY = ((StoreListViewController*)uvc).storeSearchB.frame.origin.y;
} else if ([uvc isKindOfClass:[OfferListViewController class]]){
searchBarOriginY = ((OfferListViewController*)uvc).filterSearchB.frame.origin.y;
}
if (searchBarOriginY-64.0 > 0.0000001 || searchBarOriginY-64.0 < 0.0000001) {
return true;
}
return false;
}
- (void)stoppedScrolling
{
UIViewController* uvc = [self viewController];
CGRect frame = uvc.navigationController.navigationBar.frame;
if (frame.origin.y < 20) {
[self animateNavBarTo:-(frame.size.height - 21)];
}
}
- (void)updateBarButtonItems:(CGFloat)alpha
{
UIViewController* uvc = [self viewController];
uvc.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor colorWithRed:1 green:1 blue:1 alpha:alpha] forKey:UITextAttributeTextColor];
uvc.navigationController.navigationBar.topItem.rightBarButtonItem.customView.alpha = alpha;
uvc.navigationController.navigationBar.topItem.leftBarButtonItem.customView.alpha = alpha;
}
- (void)animateNavBarTo:(CGFloat)y
{
UIViewController* uvc = [self viewController];
[UIView animateWithDuration:0.2 animations:^{
CGRect frame = uvc.navigationController.navigationBar.frame;
CGFloat alpha = (frame.origin.y >= y ? 0 : 1);
frame.origin.y = y;
[uvc.navigationController.navigationBar setFrame:frame];
[self updateBarButtonItems:alpha];
}];
}
- (CGRect)setNeedToHideOrShowUIViewFrame:(CGRect)frame scrollView:(UIScrollView *)scrollView
{
CGFloat size = frame.size.height - 21;
if (scrollView.contentOffset.y+152 <= 0.0f) {
frame.origin.y = 20;
} else {
frame.origin.y = MAX(-size, 20-(152+scrollView.contentOffset.y));
}
return frame;
}
- (CGRect)setNeedToHideOrShowUISearchFrame:(CGRect)frame scrollView:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.y+152 <= 0.0f) {
frame.origin.y = 64;
} else {
frame.origin.y = 64-(152+scrollView.contentOffset.y);
}
return frame;
}
-(void)navigationBarAndSearchBarHideOrShow:(UIScrollView *)scrollView {
UIViewController* viewController = [self viewController];
CGRect frame = viewController.navigationController.navigationBar.frame;
CGFloat framePercentageHidden = ((20 - frame.origin.y) / (frame.size.height - 1));
if ([viewController isKindOfClass:[StoreListViewController class]]) {
CGRect storesearchBFrame = ((StoreListViewController*)viewController).storeSearchB.frame;
storesearchBFrame = [self setNeedToHideOrShowUISearchFrame:storesearchBFrame scrollView:scrollView];
[((StoreListViewController*)viewController).storeSearchB setFrame:storesearchBFrame];
}
else if ([viewController isKindOfClass:[OfferListViewController class]]){
CGRect offersearchBFrame = ((OfferListViewController*)viewController).filterSearchB.frame;
offersearchBFrame = [self setNeedToHideOrShowUISearchFrame:offersearchBFrame scrollView:scrollView];
[((OfferListViewController*)viewController).filterSearchB setFrame:offersearchBFrame];
}
[self updateBarButtonItems:(1 - framePercentageHidden)];
frame = [self setNeedToHideOrShowUIViewFrame:frame scrollView:scrollView];
[viewController.navigationController.navigationBar setFrame:frame];
NSInteger topValue = (NSInteger)[self getListViewTop];
// NSLog(@"topValue : %i", topValue);
self.listView.contentInset = UIEdgeInsetsMake(topValue, 0, 0, 0);
lastOffset.y = scrollView.contentOffset.y;
}
- (NSInteger) countOfRows {
NSInteger previousOpenSectionIndex = self.openSectionIndex;
if (previousOpenSectionIndex != NSNotFound) {
SectionData *previousOpenSection = [self.listData objectAtIndex:previousOpenSectionIndex];
NSInteger countOfRows = [previousOpenSection.cells count];
return countOfRows;
}
return 0;
}
- (BOOL) isKindOfStoreOrOffer
{
UIViewController *uvc = [self viewController];
if ([uvc isKindOfClass:[StoreListViewController class]] || [uvc isKindOfClass:[OfferListViewController class]]) {
return true;
}
return false;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0") && [self isKindOfStoreOrOffer]) {
[self navigationBarAndSearchBarHideOrShow:scrollView];
}
[self.slimeRefreshV scrollViewDidScroll];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
willDecelerate:(BOOL)decelerate
{
[self.slimeRefreshV scrollViewDidEndDraging];
}
merchantDetail
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setupNavBarWithBackBtn:self.merchantName];
[self.merchantSegmentBar addTarget:self
action:@selector(segmentControlValueChanged:)
forControlEvents:UIControlEventValueChanged];
_contentListV.listDelegate = self;
CGRect frame = self.navigationController.navigationBar.frame;
frame.origin.y = 20;
[self.navigationController.navigationBar setFrame:frame];
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
[_contentListV setupCollapseListView:YES contentInset:UIEdgeInsetsMake(108, 0, 0, 0)];
} else {
[_contentListV setupCollapseListView:YES];
}
_contentListData = [[NSMutableArray alloc] init];
TaskItem* t = [[TaskItem alloc] initTask:@selector(doInitFetch)
forTarget:self
withInfo:nil
andObserver:self];
[[TaskCenter sharedTaskCenter] addTask:t];
[t release];
}
merchantlist
- (void)stoppedScrolling
{
CGRect frame = self.navigationController.navigationBar.frame;
if (frame.origin.y < 20) {
[self animateNavBarTo:-(frame.size.height - 21)];
}
}
- (void)updateBarButtonItems:(CGFloat)alpha
{
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor colorWithRed:1 green:1 blue:1 alpha:alpha] forKey:UITextAttributeTextColor];
self.navigationController.navigationBar.topItem.rightBarButtonItem.customView.alpha = alpha;
self.navigationController.navigationBar.topItem.leftBarButtonItem.customView.alpha = alpha;
}
- (void)animateNavBarTo:(CGFloat)y
{
[UIView animateWithDuration:0.2 animations:^{
CGRect frame = self.navigationController.navigationBar.frame;
CGFloat alpha = (frame.origin.y >= y ? 0 : 1);
frame.origin.y = y;
[self.navigationController.navigationBar setFrame:frame];
[self updateBarButtonItems:alpha];
}];
}
- (CGRect)setNeedToHideOrShowUIViewFrame:(CGRect)frame scrollView:(UIScrollView *)scrollView
{
CGFloat size = frame.size.height - 21;
if (scrollView.contentOffset.y+152 <= 0.0f) {
frame.origin.y = 20;
} else {
frame.origin.y = MAX(-size, 20-(152+scrollView.contentOffset.y));
}
return frame;
}
- (CGRect)setNeedToHideOrShowUISearchFrame:(CGRect)frame scrollView:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.y+152 <= 0.0f) {
frame.origin.y = 64;
} else {
frame.origin.y = 64-(152+scrollView.contentOffset.y);
}
return frame;
}
- (CGFloat)getListViewTop
{
CGFloat searchBarOriginY = 0.0;
CGFloat searchBarSizeHeight = 0.0;
searchBarOriginY = self.merchantSearchB.frame.origin.y;
searchBarSizeHeight = self.merchantSearchB.frame.size.height;
if (searchBarSizeHeight + searchBarOriginY >= 20.0) {
return (searchBarOriginY + searchBarSizeHeight) > 152.0 ? 152 : (searchBarOriginY + searchBarSizeHeight);
}
return 20.0;
}
-(void)navigationBarAndSearchBarHideOrShow:(UIScrollView *)scrollView {
CGRect frame = self.navigationController.navigationBar.frame;
CGRect merchantSearchBarFrame = self.merchantSearchB.frame;
CGFloat framePercentageHidden = ((20 - frame.origin.y) / (frame.size.height));
[self.navigationController.navigationBar setFrame:[self setNeedToHideOrShowUIViewFrame:frame scrollView:scrollView]];
[self.merchantSearchB setFrame:[self setNeedToHideOrShowUISearchFrame:merchantSearchBarFrame scrollView:scrollView]];
[self updateBarButtonItems:(1 - framePercentageHidden)];
CGFloat topinset = [self getListViewTop];
self.merchantListV.contentInset = UIEdgeInsetsMake(topinset, 0, 0, 0);
lastOffset.y = scrollView.contentOffset.y;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
// [self changeSearchBar:self.merchantSearchB withTintColor:SEARCHBAR_TINT_IOS7 scopeTintColor:SCOPEBAR_TINT_IOS7];
[self navigationBarAndSearchBarHideOrShow:scrollView];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment