how to change UISearchBar from round to rectangle?

后端 未结 12 1541
没有蜡笔的小新
没有蜡笔的小新 2021-02-09 01:54

i will like to know how do i need to change a UISearchBar from the default round curve to a rectangle.

\"enter

相关标签:
12条回答
  • 2021-02-09 02:21

    I also recently achieved this in a project by traversing the subviews of the UISearchBar object. However, the text field was not an immediate child as the accepted answer suggested, but a subview of another subview of it. The inner view hierarchy was probably changed at some point. (SDK 8.4)

    + (void)setSearchBar:(UISearchBar *)searchBar cornerRadius:(CGFloat)radius {
        //set searchbar corner radius
        for(UIView *possibleSearchBarTextFieldSuperview in [searchBar subviews]) {
            if([[possibleSearchBarTextFieldSuperview subviews] count] > 0) {
                for(UIView *possibleInnerSearchBarTextField in [possibleSearchBarTextFieldSuperview subviews]) {
                    if([possibleInnerSearchBarTextField conformsToProtocol:@protocol(UITextInputTraits)]) {
                        possibleInnerSearchBarTextField.layer.cornerRadius = radius;
                        possibleInnerSearchBarTextField.layer.masksToBounds = YES;
                        return;
                    }
                }
            }
        }
    }
    

    This method does not use any undocumented methods so it's probably App Store safe, although it is susceptible to stop working with future changes to the SDK.

    0 讨论(0)
  • 2021-02-09 02:23

    Why not use the UIAppearance protocol?

    [[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setBorderStyle:UITextBorderStyleRoundedRect];

    0 讨论(0)
  • 2021-02-09 02:25

    I would use a UIView, with these features:
    - set your frame.
    - import CoreGraphics in your class
    - set a white background of the view
    - set view.layer.cornerRadius=10;
    - set view.layer.borderWidth=8;
    - set view.layer.borderColor=[UIColor blackColor].CGColor;
    - insert a label(with a clear background) inside the created view
    For the button on the left i would use an uiimage, for the right button set textField.clearButtonMode= UITextFieldViewModeAlways;

    Hope this helps.

    0 讨论(0)
  • 2021-02-09 02:26

    In our earlier project we also tried to implement in such manner but customization is not possible.

    0 讨论(0)
  • 2021-02-09 02:30

    Could you try this.

        for (UIView *searchBarSubview in [search_bar subviews]) {
            if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)]) {
                if([searchBarSubview isKindOfClass:[UITextField class]])
                {
                    [(UITextField *)searchBarSubview setBorderStyle:UITextBorderStyleRoundedRect];
                }
            }
        }
    
    0 讨论(0)
  • 2021-02-09 02:35

    The way that it worked with me on Swift 4 is to Subclass the UISearchBar and make the necessary adjustment there without any kind of hacking or workaround ... I have used the Subclass used in this tutorial and it worked like charm

    Here is the code from the example used

    //
    //  SearchBar.swift
    //  Yomu
    //
    //  Created by Sendy Halim on 9/3/17.
    //  Copyright © 2017 Sendy Halim. All rights reserved.
    //
    
    import Foundation
    import UIKit
    
    class SearchBar: UISearchBar {
      override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)
    
        searchBarStyle = .minimal
    
        // Create search icon
        let searchIcon = UIImageView(image: #imageLiteral(resourceName: "search"))
        let searchImageSize = searchIcon.image!.size
        searchIcon.frame = CGRect(x: 0, y: 0, width: searchImageSize.width + 10, height: searchImageSize.height)
        searchIcon.contentMode = UIViewContentMode.center
    
        // Configure text field
        let textField = value(forKey: "_searchField") as! UITextField
        textField.leftView = searchIcon
        textField.borderStyle = .none
        textField.backgroundColor = UIColor(hex: "#F7F7F7")
        textField.clipsToBounds = true
        textField.layer.cornerRadius = 6.0
        textField.layer.borderWidth = 1.0
        textField.layer.borderColor = textField.backgroundColor!.cgColor
        textField.textColor = UIColor(hex: "#555555")
      }
    }
    
    0 讨论(0)
提交回复
热议问题