2016-08-11 7 views
2

私のiOSアプリケーションでは、にUISearchBarUIButtonのナビゲーションバーがあります。私はストーリーボードができないので、UISearchController私は空白のUIViewをスタックビューに持っており、私はUISearchBarをサブビューとして追加しています。ここに私のストーリーボードは次のようになります。enter image description here私のUISearchBarが制約に従わないのはなぜですか?

ここに私のコードでは、検索バー

override func viewDidLoad() { 
    super.viewDidLoad() 
    configureSearchController() 
    print(wrapperView.bounds) 
    print(searchController.searchBar.bounds) 
} 

func configureSearchController() { 
    searchController = UISearchController(searchResultsController: nil) 
    searchController.dimsBackgroundDuringPresentation = false 
    searchController.searchBar.placeholder = "Search here..." 
    searchController.searchBar.sizeToFit() 
    searchController.searchBar.isTranslucent = false 
    searchController.searchBar.delegate = self 
    searchController.delegate = self 
    searchController.searchBar.barTintColor = addButton.backgroundColor 
    wrapperView.addSubview(searchController.searchBar) 
    let color = addButton.backgroundColor 
    searchController.searchBar.layer.borderWidth = 1 
    searchController.searchBar.layer.borderColor = color?.cgColor 
    searchController.searchBar.trailingAnchor.constraint(equalTo: addButton.leadingAnchor) 
} 

を追加することです。しかし、ここで示したように、検索バーは、UIButton過ぎて行く: enter image description here 私はUISearchBarに取得できますかUIButtonで終わりますか? enter image description here

+0

uiobjectリストのラッパービューの上にボタンを移動してください。 –

+0

@SaintThreadこれは、検索バーの前に 'UIButton'を配置します。 –

+0

申し訳ありませんが、私は質問を誤解、あなたはラッパービューの制約を使用することができますか?たぶんあなたは親ビューの後ろにボタンがありません。 –

答えて

0

交換してみてください:

searchController.searchBar.trailingAnchor.constraint(equalTo: addButton.leadingAnchor) 

searchController.translatesAutoresizingMaskIntoConstraints = false 
wrapperView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: nil, metrics: nil, views: ["subview": searchController])) 
wrapperView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-0-|", options: nil, metrics: nil, views: ["subview": searchController])) 

EDIT: そうでない場合はUISearchControllerで試してみてください。

searchController.searchBar.setImage(<image_name>, forSearchBarIcon: .Bookmark, state: .Normal) 

これはアロますあなたは検索バーの近くにカスタムボタンを追加します。

+0

スウィフト3は、オプション021を打ち切らせません。 –

+0

空の配列を試してください: '[]' –

+0

空の配列を取りました。アプリケーションがクラッシュする –

0

同じ問題が発生しましたが、私はsearchController.searchBar.sizeToFit()を私のビュー(addSubview()の直後)に追加して修正することができました。あなたは前にwrapperView.layoutIfNeeded()を実行する必要がある場合がありますので、このような何か:

wrapperView.addSubview(searchController.searchBar) 
wrapperView.layoutIfNeeded() 
searchController.searchBar.sizeToFit() 

はそれが役に立てば幸い!

関連する問題