【问题标题】:how to add/show toolbar below status bar in iOS?如何在 iOS 的状态栏下方添加/显示工具栏?
【发布时间】:2016-12-08 19:31:07
【问题描述】:

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

@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*/];
}

【问题讨论】:

  • 请出示您的代码
  • @Umair Afzal - 我已经发布了代码。请看一下
  • @Sanket,为什么不使用 UINavigationController?
  • 如果你不想要导航栏,你应该使用navigation controllerUIView!你为什么用toolbar

标签: ios objective-c


【解决方案1】:

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

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

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

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

这是我在视图中加载的更新代码。剩下的都是一样的

- (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*/];
}

最终结果

【讨论】:

    【解决方案2】:

    不要从TopLayoutGuide.Bottom 固定约束,而是用superview's top 固定它。你的toolbar's top 应该用superview's top 固定。看看下面的截图,

    【讨论】:

    • 是的,先生,我知道了。这次我正确放置。但同样的问题仍然存在。
    • 你想达到什么目的?你想隐藏工具栏吗?还是只想向上拖动工具栏?工具栏的高度是多少?工具栏的y 位置是多少?
    • myToolbarFrame = ((0, 20),(widthOfView, 44))
    • 为了达到我的目的,我正在关注这个链接stackoverflow.com/questions/19819165/…,但我正在更改工具栏的代码而不是导航控制器的导航栏
    • 我想克隆 facebook iOS 应用程序工具栏的行为。在滚动时,我想要“以与拖动速率成比例的速率隐藏/显示工具栏”。我还想“在栏缩小时淡化工具栏及其项目”。但是当工具栏向上移动时,它的内容与状态栏的内容混合在一起
    猜你喜欢
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 2016-10-05
    相关资源
    最近更新 更多