-1

I have found a good answer about this problem, look at here How to hide iOS7 UINavigationBar 1px bottom line

but i want to know how to implement it with swift, i've tried in this way

func findHairlineImageViewUnder(view:UIView!) {
    if  view is UIImageView && view.bounds.size.height <= 1.0 {
        return view
    }

    var subview: UIView

    for subview in view.subviews {
        var imageView:UIImageView = self.findHairlineImageViewUnder(subview)
        if imageView {
            return imageView
        }
    }
}

i can't make it because the compiler told me

  1. 'UIView' is not convertible to ()
  2. cannot convert the expression's type 'AnyObject' to type 'UIImageView'
  3. Type 'UIImageView' does not conform to protocol 'BoooleanType'

i know why these errors came out but how can i fix it?

Community
  • 1
  • 1

2 Answers2

2

This extension should do it.

extension UINavigationController {

func hairLine(hide hide: Bool) {
    //hides hairline at the bottom of the navigationbar
    for subview in self.navigationBar.subviews {
        if subview.isKindOfClass(UIImageView) {
            for hairline in subview.subviews {
                if hairline.isKindOfClass(UIImageView) && hairline.bounds.height <= 1.0 {
                    hairline.hidden = hide
                }
            }
        }
    }

}
}

Just call it like this:

navigationController?.hairLine(hide: true)
dmlebron
  • 861
  • 6
  • 16
0

There are a few problems in your code.

  1. You haven't specified a return type, so the compiler is trying to figure it out for you but is having trouble. You should specify that your return type is UIView?; it should be an optional because you may not be able to find the view and will need to return nil.
  2. You don't need to declare subview before your for loop; the for loop will do it implicitly.
  3. Declaring imageView to a UIImageView will not work without casting. However, you don't really need to make it a UIImageView in the first place and you can let swift deal with it.
  4. By making the change in 1, you can now simplify the inside of your for loop using optional binding and just do if let foundView = self.findHairlineImageViewUnder(subview) { ... }
  5. If you never find the view you're looking for, you never return anything from the function. That's going to cause a compile error in swift, so make sure to return nil at the very end.

Here is a working implementation with the above fixes:

func findHairlineImageViewUnder(view:UIView!) -> UIView? {
    if view is UIImageView && view.bounds.size.height <= 1.0 {
        return view
    }

    for subview in view.subviews as [UIView] {
        if let foundView = self.findHairlineImageViewUnder(subview) {
            return foundView
        }
    }

    return nil
}
Mike S
  • 41,895
  • 11
  • 89
  • 84