iPhone X - Set the color of the area around home indicator

十年热恋 提交于 2019-12-05 15:25:15

Here is my tiny extension. If anybody can suggest an improvement to not access added view by "magic number" tag - welcome!

extension UIViewController {

private static let insetBackgroundViewTag = 98721 //Cool number

func paintSafeAreaBottomInset(withColor color: UIColor) {
    guard #available(iOS 11.0, *) else {
        return
    }
    if let insetView = view.viewWithTag(UIViewController.insetBackgroundViewTag) {
        insetView.backgroundColor = color
        return
    }

    let insetView = UIView(frame: .zero)
    insetView.tag = UIViewController.insetBackgroundViewTag
    insetView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(insetView)
    view.sendSubview(toBack: insetView)

    insetView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    insetView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    insetView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    insetView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true

    insetView.backgroundColor = color
}

}

Yes, it's not a good idea but can be a solution to your problem.

Follow this steps:

  • Enable 'Safe Area Layout'
  • Create/add a view (i.e. name it bottomPaddingView) (Programtically or using storyboard) with background color, same as your sheet color.
  • Position it (using following constraints - following constraints are not a code structure, but just showing a constraint/anchor relationship)
    - bottomPaddingView.bottom = self.view.bottom
    - bottomPaddingView.trailing = self.view.trailing
    - bottomPaddingView.leading = self.view.leading
    - bottomPaddingView.top = self.view.safeAreaLayoutConstraint.bottom // If you've enabled 'Safe Area Layout'
    - or bottomPaddingView.top = self.view.bottomLayoutguide // If you've not enabled 'Safe Area Layout'

Now hide/unhide your bottomPaddingView with respect to actionship visibility with fade animation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!