Can the height of the UISearchbar TextField be modified?

后端 未结 8 862
-上瘾入骨i
-上瘾入骨i 2020-12-05 04:34

I\'m testing out my UI and I find the search bar a little bit too narrow for my liking. I also want to make sure people with poorer vision or poorer manual dexterity have n

相关标签:
8条回答
  • 2020-12-05 04:42

    If you use :

    func setSearchFieldBackgroundImage(backgroundImage: UIImage?, forState state: UIControlState)   
    

    The height of UISearchBar's UITextfield would be the same as the height of the Image.

    You can drop in an Image,use it to customize UISearchBar's UITextfield height and backgroundImage.

    Or you can use the function below to create a round corner UIImage:

    func getImageWithColor(color: UIColor, size: CGSize) -> UIImage {
        let rect = CGRectMake(0, 0, size.width, size.height)
        let path = UIBezierPath(roundedRect: rect, cornerRadius: 5.0)
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        color.setFill()
        path.fill()
        let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
    

    The Width of the UIImage is ignored.But to have a corner radius ,it's better bigger than 10.0.

    The code would be look like this:

    let image = self.getImageWithColor(UIColor.lightGrayColor(), size: CGSizeMake(20, 20))
    searchBar.setSearchFieldBackgroundImage(image, forState: .Normal)
    
    0 讨论(0)
  • 2020-12-05 04:43

    I've found the accepted answer to be buggy at times.
    This is how i've solved the issue of having a custom height for the searchBar: I have set a colour image with the size I want and added it to the searchBar's textField in viewDidLoad:

    let image = getImageWithColor(UIColor.whiteColor(), size: CGSizeMake(600, 60))
    searchBar.setSearchFieldBackgroundImage(image, forState: .Normal)
    

    Followed with this func taken from here

    func getImageWithColor(color: UIColor, size: CGSize) -> UIImage {
        var rect = CGRectMake(0, 0, size.width, size.height)
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        color.setFill()
        UIRectFill(rect)
        var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
    
    0 讨论(0)
  • 2020-12-05 04:46
        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 :)

    0 讨论(0)
  • 2020-12-05 04:54

    Based on thelearner's answer here's the code for Swift 5.* :

    extension UISearchBar {
        func updateHeight(height: CGFloat, radius: CGFloat = 8.0) {
            let image: UIImage? = UIImage.imageWithColor(color: UIColor.clear, size: CGSize(width: 1, height: height))
            setSearchFieldBackgroundImage(image, for: .normal)
            for subview in self.subviews {
                for subSubViews in subview.subviews {
                    if #available(iOS 13.0, *) {
                        for child in subSubViews.subviews {
                            if let textField = child as? UISearchTextField {
                                textField.layer.cornerRadius = radius
                                textField.clipsToBounds = true
                            }
                        }
                        continue
                    }
                    if let textField = subSubViews as? UITextField {
                        textField.layer.cornerRadius = radius
                        textField.clipsToBounds = true
                    }
                }
            }
        }
    }
    
    private extension UIImage {
        static func imageWithColor(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)
            guard let image: UIImage = UIGraphicsGetImageFromCurrentImageContext() else {
                return nil
            }
            UIGraphicsEndImageContext()
            return image
        }
    }
    

    and here's how we can apply it:

    searchBar?.setSearchTextField(height: 48)
    

    and here's the result:

    0 讨论(0)
  • 2020-12-05 04:58

    For swift 5.0 it works good:

    extension UIImage {
        class func image(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) -> UIImage {
            return UIGraphicsImageRenderer(size: size).image { rendererContext in
                color.setFill()
                rendererContext.fill(CGRect(origin: .zero, size: size))
            }
        }
    }
    
    extension UISearchBar {
        func customize() {
            setSearchFieldBackgroundImage(UIImage.image(color: .clear, size: CGSize(width: 1, height: 44)), for: .normal)
    
            // other settings
            searchBarStyle = .minimal
            searchTextPositionAdjustment = UIOffset(horizontal: 0, vertical: 0)
            tintColor = .black
            backgroundColor = .whiteBackground
            searchTextField.leftViewMode = .never
            searchTextField.textColor = .black
            searchTextField.backgroundColor = UIColor.lightGrayBackground
            searchTextField.layer.borderColor = UIColor.gray.cgColor
            searchTextField.cornerRadius = 22
            searchTextField.borderWidth = 0.5
        }
    }
    

    Just use:

    let searchBar = UISearchBar()
    searchBar.customize()
    
    0 讨论(0)
  • 2020-12-05 04:59

    You can change the height of the inner text field try this code.

    step 1) set constraint to increase your UISearchBar height then make outlet of search bar and write down this code in view controller.

    - (void)viewDidLayoutSubviews
    {
        [super viewDidLayoutSubviews];
    
        [self.searchBar layoutSubviews];
    
        float topPadding = 5.0;
        for(UIView *view in self.searchBar.subviews)
        {
            for(UITextField *textfield in view.subviews)
            {
                if ([textfield isKindOfClass:[UITextField class]]) {
                    textfield.frame = CGRectMake(textfield.frame.origin.x, topPadding, textfield.frame.size.width, (self.searchBar.frame.size.height - (topPadding * 2)));
                }
            }
        }
    }
    

    also if you want to change anything else in the search bar then you can find the exact base element like UILabel, UIImage and UIView inside the UITextField subviews and can change the frame and image and other properties also. Thanks.

    EDIT:

    as you asked in swift am writing it in swift. i think you are trying something wrong. please try this.

    func viewDIdLayoutSubviews()
    {
        super.viewDidLayoutSubviews()
        self.roomInfoSearchBar.layoutSubviews()
    
        var topPadding: Float = 5.0
        for view in self.roomInfoSearchBar.subviews as [UIView] 
        {
            for viewNew in view.subviews as [UIView] 
            {
                if viewNew.isKindOfClass(UITextField) 
                {
                    viewNew.frame = CGRectMake(viewNew.frame.origin.x, CGFloat(topPadding), viewNew.frame.size.width, self.roomInfoSearchBar.frame.size.height - CGFloat(topPadding * 2))
                }
            }
        }
    }
    

    you taken the 0 index view as first and last view to be looped. i think thats the problem the code you translated didn't worked. Try once and please reply.

    0 讨论(0)
提交回复
热议问题