Set background color for top safe area only in iOS 11

Viewed 8032

I'm new to Swift. I have set my top bar programmatically.It works fine for all versions except for iOS version 11.

this is how it looks in iphone 5s version 11

This is how it looks in iphoneX version 11

I want to change the background color of the safe area in iPhone X. Presently I have just added the following code to hide the status bar.

 override var prefersStatusBarHidden: Bool {
    return true
}

I have created top bar using code:

//Top Bar
    let topBar = UIView(frame:CGRect(x: 0,y: 0, width: width, height: 60))
    topBar.backgroundColor = UIColor.white
    topBar.layer.shadowColor = UIColor.gray.cgColor
    topBar.layer.shadowOffset = CGSize(width: 0, height: 3)
    topBar.layer.shadowOpacity = 1
    topBar.layer.masksToBounds = false
    topBar.layer.shadowRadius = 8.0;
    //ImageView - Back Button
    let backBtn = UIButton(frame:CGRect(x: 25, y: 18, width: 18, height: 34))
    let backBtnImage = UIImage(named: "back_button") as UIImage?
    backBtn.setImage(backBtnImage, for: .normal)
    backBtn.layer.masksToBounds = true
    backBtn.addTarget(self,action:#selector(backButtonClicked),
                      for:.touchUpInside)
    //Label - Title
    let titleLabel = UILabel(frame:CGRect(x: width * 0.3, y: 13, width: width * 0.55, height: 40))
    titleLabel.text = "Favorites"
    titleLabel.contentMode = UIViewContentMode.center
    //include all in view
    topBar.addSubview(titleLabel)
    topBar.addSubview(backBtn)

    containerView.addSubview(topBar)

Is there a way in which i can do it without using UINavigationBar or setting status bar.

1 Answers
Related