Martin Perry
Martin Perry

Reputation: 9527

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:

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)

Open state: topInfoView.topAnchor.constraint(equalTo: mainView.topAnchor, constant: 0)

where mainView is main UIViewCcontroller view.

I set HEIGHT as my user height + "status bar height" (bar with battery, Wifi etc). Problem is, that sometimes status bar height is 0 and my topInfoView is incorrectly placed. 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. Is there any other way, without calculating the height?

Upvotes: 1

Views: 83

Answers (1)

Keshav Raj
Keshav Raj

Reputation: 476

Use the vertical stack view. Put the tableView and then your view vertically one after the another. Set the height of the table view. Keep the distribution property of stack view to fill. Create an outlet of the tableview. With this arrangement when you will hide the tableview, your view will fill the whole area. When you again set isHidden property to false of table view your table view and view will appear as original arrangement. You can animate while hiding and showing table view to give a good user experience.

Upvotes: 1

Related Questions