ios - 如何在 iOS 的状态栏下方添加/显示工具栏?

标签 ios objective-c

在我的 iOS 应用程序中,我添加了在表格 View 滚动时折叠工具栏的功能。但是当工具栏沿 y 轴移动到上方时,我得到了以下结果(工具栏内容与状态栏内容混合)。

enter image description here

@interface ListViewController () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *labelPageTitle;
@property (weak, nonatomic) IBOutlet UITableView *listTableView;
@property (nonatomic) CGFloat previousScrollViewYOffset;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *toolbarTop;

@end

@implementation ListViewController

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    CGRect frame = self.toolbars.frame;
//    CGFloat size = frame.size.height - 21;
//    CGFloat framePercentageHidden = ((20 - frame.origin.y) / (frame.size.height - 1));
//    CGFloat scrollOffset = scrollView.contentOffset.y;
//    CGFloat scrollDiff = scrollOffset - self.previousScrollViewYOffset;
//    CGFloat scrollHeight = scrollView.frame.size.height;
//    
//    NSLog(@"scrollView.frame - %@", NSStringFromCGRect(scrollView.frame));
//    NSLog(@"scrollView.contentInset - %@", NSStringFromUIEdgeInsets(scrollView.contentInset));
//    
//    CGFloat scrollContentSizeHeight = scrollView.contentSize.height + scrollView.contentInset.bottom;
//    
//    if (scrollOffset <= -scrollView.contentInset.top) {
//        frame.origin.y = 20;
//    } else if ((scrollOffset + scrollHeight) >= scrollContentSizeHeight) {
//        frame.origin.y = -size;
//    } else {
//        frame.origin.y = MIN(20, MAX(-size, frame.origin.y - scrollDiff));
//    }
//    
//    [self.toolbars setFrame:frame];
//    [self updateBarButtonItems:(1 - framePercentageHidden)];
//    self.previousScrollViewYOffset = scrollOffset;
//}
//
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
//{
//    [self stoppedScrolling];
//}
//
//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
//                  willDecelerate:(BOOL)decelerate
//{
//    if (!decelerate) {
//        [self stoppedScrolling];
//    }
//}
//
//- (void)stoppedScrolling
//{
//    CGRect frame = self.navigationController.navigationBar.frame;
//    if (frame.origin.y < 20) {
//        [self animateNavBarTo:-(frame.size.height - 21)];
//    }
//}
//
//- (void)updateBarButtonItems:(CGFloat)alpha
//{
//    self.buttonDismiss.customView.alpha = alpha;
//    self.labelPageTitle.alpha = alpha;
//    self.toolbars.tintColor = [self.toolbars.tintColor colorWithAlphaComponent:alpha];
//}
//
//- (void)animateNavBarTo:(CGFloat)y
//{
//    [UIView animateWithDuration:0.2 animations:^{
//        CGRect frame = self.toolbars.frame;
//        CGFloat alpha = (frame.origin.y >= y ? 0 : 1);
//        frame.origin.y = y;
//        [self.toolbars setFrame:frame];
//        [self updateBarButtonItems:alpha];
//    }];
//}

#pragma mark - view controllers life cycle methods

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
        self.edgesForExtendedLayout = UIRectEdgeNone;

    self.extendedLayoutIncludesOpaqueBars=NO;
    self.automaticallyAdjustsScrollViewInsets=NO;

    [self.view layoutIfNeeded];

    _toolbarTop.constant = -34;
    [self.listTableView setDataSource:self];
    [self.listTableView setDelegate:self];

    [Utils updateLabelFontSize:self.labelPageTitle ForInitialHeight:22 andInitialSize:21];

    [self.labelPageTitle setText:@"My Category"/*self.productCategory*/];
}

最佳答案

最后玩了4-5个小时后,我遇到了解决方案。首先感谢@Lion 和@Desdenova 的帮助。

这是我找到一些提示的链接。

iOS8: How do I make statusBar opaque after navigationBar is hidden using hidesBarsOnSwipe?

根据帖子中的建议,我刚刚在状态栏框架上分配了一个具有相同颜色的工具栏色调的 UIView。

这是我在 View 中更新的代码确实加载了。其余相同

- (void)viewDidLoad {
    [super viewDidLoad];
//    if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
//        self.edgesForExtendedLayout = UIRectEdgeNone;
//    
//    self.extendedLayoutIncludesOpaqueBars=NO;
//    self.automaticallyAdjustsScrollViewInsets=NO;

    [self.view layoutIfNeeded];

    _toolbarTop.constant = -34;
    [self.listTableView setDataSource:self];
    [self.listTableView setDelegate:self];

    //let topBar = UIView(frame: UIApplication.sharedApplication().statusBarFrame)
    UIView *statusBarView = [[UIView alloc] initWithFrame:[[UIApplication sharedApplication] statusBarFrame]];
    statusBarView.backgroundColor = self.view.backgroundColor;
    [self.view addSubview:statusBarView];

    [Utils updateLabelFontSize:self.labelPageTitle ForInitialHeight:22 andInitialSize:21];

    [self.labelPageTitle setText:@"My Category"/*self.productCategory*/];
}

最终结果

enter image description here

关于ios - 如何在 iOS 的状态栏下方添加/显示工具栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41034263/

相关文章:

iphone - SKProductsResponse 对于应用内购买没有响应

ios - 在 iOS 中使用公钥验证数字签名

iphone - 如何在 Objective C (iPhone) 中保存录制的音频

ios - -[RCTRootView cancelTouches]` 已弃用,很快就会在 React Native map 中删除

ios - 获取 UINavigationBar 委托(delegate)以允许 "back",带有漂亮的动画

android - 使用 ScrollView 的 Flex 布局

ios - youtube API 不工作 ios

javascript - 专门针对 CSS 规则而不是 chrome 在 iPhone 上针对 Safari 浏览器?

objective-c - Objective C 和 Swift 是否支持分层继承?

ios - 使用自动布局的 XIB 中的 UIView 始终为 600x600