Change height of UISearchBar scope buttons in iOS?

I would like to change the height of the scope buttons for my search bar. I am using adjusted segmented controls and would like the zoom buttons to look the same. Below is a screenshot of my sphere buttons and another screenshot of my segmented controls that I would like to map.

This is the code I'm using for a segmented control that might need to be reapplied to the search bar's scope buttons, but I don't know how:

    filterSegmentedControl.frame = CGRect(
        x: filterSegmentedControl.frame.origin.x,
        y: filterSegmentedControl.frame.origin.y,
        width: filterSegmentedControl.frame.size.width,
        height: 22)

      

My search scope buttons in the search bar: enter image description here

My segmented control: enter image description here

+3


source to share


2 answers


The segmented control does not appear inside the UISearchBar interface. Thus, you cannot control the border of the segmented control internally. If you really want to change the height of the frame, I see no other way to do it other than going through the subViews to find the UISegmentedControl and then manually setting the frame for it.

Below is a small helper extension on UIView to help you traverse the subview hierarchy inside the UISearchBar to find the UISegmentedControl.

extension UIView {

    func traverseSubviewForViewOfKind(kind: AnyClass) -> UIView? {
        var matchingView: UIView?

        for aSubview in subviews {
            if aSubview.dynamicType == kind {
                matchingView = aSubview
                return matchingView
            } else {
                if let matchingView = aSubview.traverseSubviewForViewOfKind(kind) {
                    return matchingView
                }
            }
        }

        return matchingView
    }
}

      



You can then use this method to manually set the border to the segmented control found, which would be something like this:

if let segmentedControl = searchBar.traverseSubviewForViewOfKind(UISegmentedControl.self) {
  var segmentedControlFrame = segmentedControl.frame
  segmentedControlFrame.size.height = 70
  segmentedControl.frame = segmentedControlFrame
}

      

+4


source


You can access the left view of the image as shown below a subset of the UITextField of the search bar



if let textField: UITextField = self.searchBar?.valueForKey("searchField") as? UITextField {
 if let searchIconImageView = textField.leftView as? UIImageView {
            searchIconImageView.frame = CGRect(x:0,y:0,width:14,height:14)
        }
}

      

0


source







All Articles