UISearchBar increases navigation bar height in iOS 11

后端 未结 19 1556
醉酒成梦
醉酒成梦 2020-11-28 02:47

I have my UISearchBar being part of the navigation bar like:

 let searchBar = UISearchBar()
 //some more configuration to the search bar
 .....
         


        
19条回答
  •  Happy的楠姐
    2020-11-28 03:17

    In my case, I have to decrease the textField's height 36pt -> 28pt.

    So I tried to change the frame's height, layer's height. But the ways didn't work.

    Finally, I found a solution that's the mask. I think, It's not a good way but it works.

        let textField              = searchBar.value(forKey: "searchField") as? UITextField
        textField?.font            = UIFont.systemFont(ofSize: 14.0, weight: .regular)
        textField?.textColor       = #colorLiteral(red: 0.1960784314, green: 0.1960784314, blue: 0.1960784314, alpha: 1)
        textField?.textAlignment   = .left
    
        if #available(iOS 11, *) {
            let radius: CGFloat           = 5.0
            let magnifyIconWidth: CGFloat = 16.0
            let inset                     = UIEdgeInsets(top: 4.0, left: 0, bottom: 4.0, right: 0)
    
            let path = CGMutablePath()
            path.addArc(center: CGPoint(x: searchBar.bounds.size.width - radius - inset.right - magnifyIconWidth, y: inset.top + radius), radius: radius, startAngle: .pi * 3.0/2.0, endAngle: .pi*2.0, clockwise: false)                        // Right top
            path.addArc(center: CGPoint(x: searchBar.bounds.size.width - radius - inset.right - magnifyIconWidth, y: searchBar.bounds.size.height - radius - inset.bottom), radius: radius, startAngle: 0, endAngle: .pi/2.0, clockwise: false)  // Right Bottom
            path.addArc(center: CGPoint(x: inset.left + radius, y: searchBar.bounds.size.height - radius - inset.bottom), radius: radius, startAngle: .pi/2.0, endAngle: .pi, clockwise: false)                                                  // Left Bottom
            path.addArc(center: CGPoint(x: inset.left + radius, y: inset.top + radius),  radius: radius, startAngle: .pi, endAngle: .pi * 3.0/2.0, clockwise: false)                                                                             // Left top
    
            let maskLayer      = CAShapeLayer()
            maskLayer.path     = path
            maskLayer.fillRule = kCAFillRuleEvenOdd
    
            textField?.layer.mask = maskLayer
        }
    

    You can change the insets, if you want to change the textField's frame.

提交回复
热议问题