Can the Height of the Uisearchbar Textfield Be Modified

Can the height of the UISearchbar TextField be modified?

    override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
for subView in searchBars.subviews {
for subsubView in subView.subviews {
if let textField = subsubView as? UITextField {
var bounds: CGRect
bounds = textField.frame
bounds.size.height = 35 //(set height whatever you want)
textField.bounds = bounds
textField.borderStyle = UITextBorderStyle.RoundedRect
// textField.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
textField.backgroundColor = UIColor.redColor()
// textField.font = UIFont.systemFontOfSize(20)
}
}
}

}

This might helps you :)

Can't Change Height of UISearchBar

I managed to increase the height of UISearchBar by adding a background image, to resize the entire search bar frame, and then managed to resize textField by changing the font size.

First change the search bar height constraint:

searchBar.heightAnchor.constraint(equalToConstant: 130).isActive = true

Create the function that will programatically create a background image for the search bar:

func getImageWithCustomColor(color: UIColor, size: CGSize) -> UIImage {
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}

Then change font size on viewDidLayoutSubviews:

override func viewDidLayoutSubviews() {
self.searchBar.layoutIfNeeded()
self.searchBar.layoutSubviews()


self.searchBar.searchTextField.font = .systemFont(ofSize: 60.0)
//Your custom text size

searchBar.layoutIfNeeded()
searchBar.layoutSubviews()
}

Finally, in viewDidLoad create an image of the same size as the desired textField custom size (your case, 130) and call the function to place the background image of the desired color on the search Bar (this case, a clear background):

let backgroundImage = getImageWithCustomColor(color: UIColor.clear, size: CGSize(width: 265, height: 130))

searchBar.setSearchFieldBackgroundImage(backgroundImage, for: .normal)

This is the output

Sample Image

Hope this helps.

Changing height of UITextField in default UIsearchBar

Use this code it works:

for subView in search_bar.subviews where subView is UITextField {
subView.frame = CGRect(x: 0, y: 0, width: subView.frame.size.width, height: 70)
}

Changing the size of the UISearchBar TextField?

it's much easier than all these suggestions. In interface builder, instead of putting the Search Bar as the header of your Table View, you can put a View instead. Then, put a Navigation Bar inside this View. Grab the left resizing handle of the Navigation Bar and pull it to the right until the N B is only 25 pixels wide. Clear out the Title in the N B (double click to select it, then delete). Then, add a Search Bar into the same View. Move its right resizing handle to the left, adjust so that it abuts the N B. That's it.

You can enable a cancel button if you want too and it also won't overlap the index (remains within the search bar).

Apparently a Table View can only have 1 subview in its header, that's why you need to put the View first, then the N B and Search Bar inside it.

UPDATE: see Beginning iPhone Development from Apress, p. 241 of SDK 3 edition. You just disable the index while searching.

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
if (isSearching) {
return nil;
}
return keys;
}

Also they talk about adding a magnifying glass to the top of the index.

Great book all around.

how to change height of search bar in swift

try this!

    for myView in searchBars.subviews  {
for mySubView in myView.subviews {
if let textField = mySubView as? UITextField {
var bounds: CGRect
bounds = textField.frame
bounds.size.height = 40 //(set your height)
textField.bounds = bounds
textField.borderStyle = UITextBorderStyle.RoundedRect
}
}
}


Related Topics



Leave a reply



Submit