简体   繁体   English

iOS - 屏幕顶部可隐藏的 UIView

[英]iOS - hideable UIView at the top of the screen

I want to create an info view that will show at the top of the screen and disappear after some time, and later it can show again and so on.我想创建一个信息视图,该视图将显示在屏幕顶部并在一段时间后消失,稍后它可以再次显示等等。

I have created UIView and set constraints:我创建了 UIView 并设置了约束:

topInfoView.leadingAnchor.constraint(equalTo: mainView.leadingAnchor)
topInfoView.trailingAnchor.constraint(equalTo: smainView.trailingAnchor)
topInfoView.heightAnchor.constraint(equalToConstant: HEIGHT)

Closed state: topInfoView.topAnchor.constraint(equalTo: mainView.topAnchor, constant: -HEIGHT)关闭状态:topInfoView.topAnchor.constraint(equalTo: mainView.topAnchor, constant: -HEIGHT)

Open state: topInfoView.topAnchor.constraint(equalTo: mainView.topAnchor, constant: 0)打开状态:topInfoView.topAnchor.constraint(equalTo: mainView.topAnchor, constant: 0)

where mainView is main UIViewCcontroller view.其中mainView是主UIViewCcontroller视图。

I set HEIGHT as my user height + "status bar height" (bar with battery, Wifi etc).我将HEIGHT设置为我的用户高度 +“状态栏高度”(带电池、Wifi 等的栏)。 Problem is, that sometimes status bar height is 0 and my topInfoView is incorrectly placed.问题是,有时状态栏高度为 0 并且我的 topInfoView 放置不正确。 I am obtaining "status bar height" via this:我通过这个获得“状态栏高度”:

func statusBarHeight() -> CGFloat {
    let statusBarSize = UIApplication.shared.statusBarFrame.size
    return Swift.min(statusBarSize.width, statusBarSize.height)
}

but it sometimes not works (views are not inited?) and I am also not sure about new iPhone X, where status bar is solved differently.但它有时不起作用(视图没有初始化?)而且我也不确定新的 iPhone X,其中状态栏的解决方式不同。 Is there any other way, without calculating the height?有没有其他方法,不计算高度?

Use the vertical stack view.使用垂直堆栈视图。 Put the tableView and then your view vertically one after the another.将 tableView 和您的视图一个接一个地垂直放置。 Set the height of the table view.设置表格视图的高度。 Keep the distribution property of stack view to fill.保持堆栈视图的分布属性进行填充。 Create an outlet of the tableview.创建 tableview 的出口。 With this arrangement when you will hide the tableview, your view will fill the whole area.通过这种安排,当您隐藏 tableview 时,您的视图将填充整个区域。 When you again set isHidden property to false of table view your table view and view will appear as original arrangement.当您再次将表视图的 isHidden 属性设置为 false 时,您的表视图和视图将显示为原始排列。 You can animate while hiding and showing table view to give a good user experience.您可以在隐藏和显示表格视图时设置动画,以提供良好的用户体验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM