以编程方式修改搜索栏高度及其文本字段高度是否是一种好习惯?

时间:2018-09-07 05:53:25

标签: ios swift

我正在使用以下代码。它似乎也不起作用。

func configureSearchController(){

resultsController.tableView.delegate = self
resultsController.tableView.dataSource = self

self.searchController = UISearchController(searchResultsController: self.resultsController)
//self.tableView.tableHeaderView = self.searchController.searchBar
self.searchController.searchResultsUpdater = self

self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.searchBar.sizeToFit()
searchController.searchBar.delegate = self
self.searchController.searchBar.scopeButtonTitles = []


for subView in searchController.searchBar.subviews {
    for subViewOne in subView.subviews {
        if subViewOne is UITextField {
            searchTextField = subViewOne as! UITextField
            subViewOne.backgroundColor = UIColor.white

            var currentTextFieldBounds = subViewOne.bounds
            currentTextFieldBounds.size.height = 45
            subViewOne.bounds = currentTextFieldBounds
            break
        }
    }
}

extendedLayoutIncludesOpaqueBars = true
definesPresentationContext = true
}

如果建议更改它们,我应该如何纠正此代码?

在Darshana的帮助下,它看上去like this,但是我想要一些like this。搜索栏处于活动状态时,其外观应为this

edited code:


var searchController : UISearchController!



     configureSearchController()



     func configureSearchController()
        {

            resultsController.tableView.delegate = self
            resultsController.tableView.dataSource = self

            self.searchController = UISearchController(searchResultsController: self.resultsController)
            //self.tableView.tableHeaderView = self.searchController.searchBar
            self.searchController.searchResultsUpdater = self

            self.searchController.dimsBackgroundDuringPresentation = false
            self.searchController.searchBar.sizeToFit()
            searchController.searchBar.delegate = self
            self.searchController.searchBar.scopeButtonTitles = []



           // searchController.searchBar.heightAnchor.constraint(equalToConstant: 70).isActive = true


            var image = self.getImageWithColor(color: UIColor.white, size:    CGSize(width: 20, height: 35))

            searchController.searchBar.setSearchFieldBackgroundImage(image, for: .normal)

            image = self.getImageWithColor(color: UIColor.gray(), size:    CGSize(width: 20, height: 35))
            searchController.searchBar.setSearchFieldBackgroundImage(image, for: .focused)

           // self.automaticallyAdjustsScrollViewInsets = false;
            extendedLayoutIncludesOpaqueBars = true
            definesPresentationContext = true

        }

添加代码不会使其处于焦点状态(活动)时更改搜索栏图像。

2 个答案:

答案 0 :(得分:1)

不建议通过遍历UISearchBar的子视图来更改UISearchBar的UITextField的高度。

为什么因为将来苹果可能会决定使用UILayer或他们想到的任何其他组件来创建UISearchBar,

如果需要,我们可以编写自己的自定义组件来解决问题。

话虽如此,如果您仍然需要更改高度,则需要像下面那样更改其约束,但不能直接为其子视图的高度分配一个值。

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

答案 1 :(得分:-1)

否,不建议通过遍历子视图来更改UIsearchbar的UITextfield的高度。

如果您确实需要更改文本字段的高度,则可以通过使用setSearchFieldBackgroundImage设置背景图像来实现。

文本字段的高度将与提供的图像的高度相同。

示例代码:Swift 4.0

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


let image = self.getImageWithColor(color: UIColor.lightGray, size: CGSize(width: 20, height: 45))
    searchBar.setSearchFieldBackgroundImage(image, for: .normal)
    searchBar.searchBarStyle = .minimal;
    searchBar.showsCancelButton  = true;

Searchbar appearance after above code