Scrolling beyond the target offset results in a weird alpha value

I have a view with a table, where there is header of size 250. I initially create a contentInset of 250 above the tableView to make space for my header. When a user scrolls past this header, I want the navigation bar to change its alpha value to 1. I.E the alpha is initially 0, at target offset, its 1. However, when I scroll too fast, or even just scroll slowly past my target offset, the navigation bar still seems a bit transparent. I want it to be fully coloured in without any transparency past a point. Here is my code in scrollViewDidScroll;

let topHeight = (self.navigationController?.navigationBar.frame.size.height)! + (UIApplication.shared.statusBarFrame.height)

    let percentageScrolledForAlphas = (250 + self.tableView.contentOffset.y)/(250 - topHeight)
    self.navAlphaComponent = CGFloat(percentageScrolledForAlphas)
    self.navigationController?.setColorToNavBar(color: #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1).withAlphaComponent(self.navAlphaComponent))

The setColorToNavBar function is as follows;

func setColorToNavBar(color: UIColor){

    let image = UIImage.imageFromColor(color: color)
    self.navigationBar.setBackgroundImage(image, for: .default)
    self.navigationBar.isTranslucent = true