UISearchBar高度自动大小更改快速

时间:2018-12-26 07:34:21

标签: ios swift uisearchbar

这是我要尝试的操作:

original image

屏幕截图来自Iphone:

second image

我正在开发一个简单的应用程序,并以编程方式添加uisearchbar,但我真的很困惑,为什么要像第二个图像一样从顶部显示额外的空间。但是当我写的时候,uisearchbar像第一张图片一样改变了我们的位置。

这是我的代码:

let searchController = UISearchController(searchResultsController: nil)

override func viewDidLoad() {
    super.viewDidLoad()

 self.searchController.obscuresBackgroundDuringPresentation = false
 self.searchController.searchBar.placeholder = "Search"
 self.searchController.searchBar.barStyle = .black
 self.searchController.searchBar.delegate = self
 self.definesPresentationContext = true
 if #available(iOS 11.0, *) {
 navigationItem.searchController = searchController
    } else {
        // Fallback on earlier versions
        navigationItem.titleView = searchController.searchBar
    }
   }

extension starControl: UISearchBarDelegate{

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar)
{
    //Show Cancel
    searchBar.setShowsCancelButton(true, animated: true)
    searchBar.tintColor = .black
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{

}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar)  
{
    searchBar.setShowsCancelButton(false, animated: true)
    searchBar.resignFirstResponder()

    guard let term = searchBar.text , term.isEmpty == false else {

        return
    }
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar)
{
    searchBar.setShowsCancelButton(false, animated: true)
    searchBar.text = String()
    searchBar.resignFirstResponder()
 }
}

有人可以向我解释如何解决此问题,我已经尝试解决此问题,但没有结果。

任何帮助将不胜感激。

谢谢。

1 个答案:

答案 0 :(得分:2)

这是最新的 swift4.2 代码和搜索栏的最新功能,只需将此功能放入控制器并从viewDidLoad调用即可。

function getResponse(srcImg,cb){

var img = new Image();
var loaded = false;
img.src = srcImg;

img.onload = function(){
  loaded = true;
  cb(loaded);
  }
 // i want to get loaded variable when the onload function is complete
 // wait the return value until the img is not loaded
 return loaded;
}

getResponse(imageURL,output(load)); //output is undefined

function output(load){
console.log(load);
}