0

To start off I already looked at this question and tried to implement it but it didn't change anything so I'll just show my own attempt in this code.

Like the question says I'm trying to change the height of the UITextField inside the searchbar. I've made a custom searchbar as well as a custom searchcontroller.

The searchbar class looks like this

class CustomSearchBar: UISearchBar {

var preferredFont: UIFont!
var preferredTextColor: UIColor!

override func drawRect(rect: CGRect) {
    // Drawing code

    // Find the index of the search field in the search bar subviews.
    if let index = indexOfSearchFieldInSubviews() {
        // Access the search field
        let searchField: UITextField = (subviews[0]).subviews[index] as! UITextField


        // Set its frame.
        //searchField.frame = CGRectMake(5.0, 5.0, frame.size.width - 10.0, frame.size.height - 10.0)
        searchField.frame = CGRectMake(5.0, 5.0, frame.size.width, 100)

        // Set the font and text color of the search field.
        searchField.font = preferredFont
        searchField.textColor = preferredTextColor

        // Set the background color of the search field.
        searchField.backgroundColor = barTintColor
    }

    let startPoint = CGPointMake(0.0, frame.size.height)
    let endPoint = CGPointMake(frame.size.width, frame.size.height)
    let path = UIBezierPath()
    path.moveToPoint(startPoint)
    path.addLineToPoint(endPoint)

    let shapeLayer = CAShapeLayer()
    shapeLayer.path = path.CGPath
    shapeLayer.strokeColor = preferredTextColor.CGColor
    shapeLayer.lineWidth = 2.5

    layer.addSublayer(shapeLayer)

    super.drawRect(rect)
}

init(frame: CGRect, font: UIFont, textColor: UIColor){
    super.init(frame: frame)

    self.frame = frame
    preferredFont = font
    preferredTextColor = textColor

    searchBarStyle = UISearchBarStyle.Prominent
    translucent = false
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

func indexOfSearchFieldInSubviews() -> Int! {
    var index: Int!
    let searchBarView = subviews[0]

    for var i=0; i<searchBarView.subviews.count; ++i {
        if searchBarView.subviews[i].isKindOfClass(UITextField) {
            index = i
            break
        }
    }

    return index
}

}

My Custom searchController looks like this

class CustomSearchController: UISearchController, UISearchBarDelegate {
var customSearchBar: CustomSearchBar!
var customDelegate: CustomSearchControllerDelegate!

init(searchResultsController: UIViewController!, searchBarFrame: CGRect, searchBarFont: UIFont, searchBarTextColor: UIColor, searchBarTintColor: UIColor){
    super.init(searchResultsController: searchResultsController)

    configureSearchBar(searchBarFrame, font: searchBarFont, textColor: searchBarTextColor, bgColor: searchBarTintColor)

}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?){
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

func configureSearchBar(frame: CGRect, font: UIFont, textColor:UIColor, bgColor: UIColor){
    customSearchBar = CustomSearchBar(frame: frame, font: font, textColor: textColor)

    customSearchBar.barTintColor = bgColor
    customSearchBar.tintColor = textColor
    customSearchBar.showsBookmarkButton = true
    customSearchBar.setImage(UIImage(named: "recording.png"), forSearchBarIcon: UISearchBarIcon.Bookmark, state: UIControlState.Normal)
    customSearchBar.setImage(UIImage(named:"record_tap.png"), forSearchBarIcon: UISearchBarIcon.Bookmark, state: UIControlState.Selected)
    customSearchBar.showsCancelButton = false

    customSearchBar.delegate = self

}

In my view controller where I'm creating this searchbar I use the following piece of code. that is being called in my viewDidLoad()

func configureCustomSearchController() {
    customSearchController = CustomSearchController(searchResultsController: self, searchBarFrame: CGRectMake(0.0, 0.0, UIScreen.mainScreen().bounds.width, 100.0), searchBarFont: UIFont(name: "Futura", size: 18.0)!, searchBarTextColor: UIColor(red: 0.612, green: 0.984, blue: 0.533, alpha: 1), searchBarTintColor: UIColor.blackColor())

    customSearchController.customSearchBar.placeholder = "Search.."
    tblSearchResults.tableHeaderView = customSearchController.customSearchBar
    customSearchController.customDelegate = self
}

I'm able to change the frame of the entire searchbar see above where I set it too 100. But I'm unable to change the height of the UITextField which I want to make bigger. I'm also sure I'm reaching the UITextView. When I change the color in the drawRect function it changes. Just the height is the problem here. Does someone have any clue what I'm doing wrong or what I need to do to achieve this.

Community
  • 1
  • 1
NoSixties
  • 2,443
  • 2
  • 28
  • 65

1 Answers1

1

I find if I move your sizing code into layoutSubviews(), I think it works as you expect.

override func layoutSubviews() {
    // Find the index of the search field in the search bar subviews.
    if let index = indexOfSearchFieldInSubviews() {
        ... //Added red background
    }
}

override func drawRect(rect: CGRect) {...

I believe by the time drawRect() is called, the time for layout has passed. I cannot really comment on if this is the best implementation though.

Reveal

Cirec Beback
  • 693
  • 10
  • 16
  • That actually worked and fixed another problem I was having thanks!! Another quick question tho. Do you know how to change the bookmark image size? if not I'll just make a new question on it – NoSixties Feb 05 '16 at 10:19
  • Always best to ask another question. I don't know off hand about changing the bookmark image. If you really want a custom look/feel, it is often easier, more maintainable, and less likely to break on an iOS update if you just build your own. – Cirec Beback Feb 05 '16 at 13:14
  • I'm not sure how I would build my own couldn't find anything on it really. I already created a new question too tho – NoSixties Feb 05 '16 at 13:23