2015-08-11 10 views
5

Chcę dodać programowo element sterujący UISearch do ograniczenia początkowego, końcowego, dolnego i szerokości. To jest mój kod:Dodawanie obiektu UISearchController i programowe programowanie ograniczeń

@IBOutlet weak var navigationBar: UIView! 

// create search bar 
searchBar = UISearchController(searchResultsController: nil) 
navigationBar.addSubview(searchBar.searchBar) 
searchBar.searchBar.translatesAutoresizingMaskIntoConstraints = false 

let leftConstraint = NSLayoutConstraint(item: searchBar.searchBar, attribute: .Leading, relatedBy: .Equal, toItem: navigationBar, attribute: .Leading, multiplier: 1, constant: 0) 

let rightConstraint = NSLayoutConstraint(item: searchBar.searchBar, attribute: .Trailing, relatedBy: .Equal, toItem: navigationBar, attribute: .Trailing, multiplier: 1, constant: 0) 

let bottomConstraint = NSLayoutConstraint(item: searchBar.searchBar, attribute: .Bottom, relatedBy: .Equal, toItem: navigationBar, attribute: .Bottom, multiplier: 1, constant: 0) 

let heightConstraint = NSLayoutConstraint(item: searchBar.searchBar, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 44) 

navigationBar.addConstraints([leftConstraint, rightConstraint, bottomConstraint, widthConstraint]) 

Po uruchomieniu aplikacji, pasek wyszukiwania pojawia się prawidłowo, ale po naciśnięciu na pasku wyszukiwania, to kurczy się, a jeśli nacisnę innym czasie ulega awarii aplikacji. Oto wynik:

2015-08-12 20:20:37.696 Contacts++[96997:8547485] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7fb22580c7a0 UIView:0x7fb225817b20.leading == UIView:0x7fb223449860.leading> 
    When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug. 
2015-08-12 20:20:37.697 Contacts++[96997:8547485] *** Assertion failure in -[UIView _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3491.2.5/NSLayoutConstraint_UIKitAdditions.m:590 
+0

To trochę dziwne nazywać cię searchController "searchBar". – dasdom

+0

Czy naprawdę ** chcesz ** ustawić *** szerokość *** navigatinBar na 44 punkty? – dasdom

+0

Mam edytować pytanie z tym, co dokładnie dzieje się teraz. – Kobe

Odpowiedz

3

Dlaczego trzeba utworzyć wystąpienie "UISearchController" i uzyskać jego searchbar?

dlaczego po prostu nie ustawiać paska wyszukiwania z UISearchBar?

// create a searchBar from UISearchBar 
let searchBar = UISearchBar(frame: CGRectZero) 

// add searchBar to navigationBar 
navigationController?.navigationBar.addSubview(searchBar) 

// call sizeToFit.. this will set the frame of the searchBar to exactly the same as the size of the allowable frame of the navigationBar 
searchBar.sizeToFit() 

// now reframe the searchBar to add some margins 
var frame = searchBar.frame 
frame.origin.x = 20 
frame.size.width -= 40 
searchBar.frame = frame // set new frame with margins 

uwaga: do tego celu nie będzie potrzebne żadne ograniczenie.

Ale jeśli naprawdę wolisz ograniczenie, oto kod ograniczenia bez awarii.

let searchBar = UISearchBar(frame: CGRectZero) 
navigationController?.navigationBar.addSubview(searchBar) 
searchBar.setTranslatesAutoresizingMaskIntoConstraints(false) 

let leftConstraint = NSLayoutConstraint(item: searchBar, attribute: .Leading, relatedBy: .Equal, toItem: navigationController?.navigationBar, attribute: .Leading, multiplier: 1, constant: 20) // add margin 

let bottomConstraint = NSLayoutConstraint(item: searchBar, attribute: .Bottom, relatedBy: .Equal, toItem: navigationController?.navigationBar, attribute: .Bottom, multiplier: 1, constant: 1) 

let topConstraint = NSLayoutConstraint(item: searchBar, attribute: .Top, relatedBy: .Equal, toItem: navigationController?.navigationBar, attribute: .Top, multiplier: 1, constant: 1) 

let widthConstraint = NSLayoutConstraint(item: searchBar, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: self.view.frame.size.width - 40) // - margins from both sides 

navigationController?.navigationBar.addConstraints([leftConstraint, bottomConstraint, topConstraint, widthConstraint]) 
+0

Zrobiłem to, dziękuję – Kobe

+0

Nie ma za co! – nferocious76

+0

Cześć, działa dobrze, ale mój pasek wyszukiwania znika, gdy go dotknę, jakiś pomysł? –

2

Czy naprawdę chcą ustawić szerokość z navigatinBar do 44 punktów? Szerokość jest pozioma. Masz już ograniczenie szerokości, dodając końcowe i wiodące ograniczenia.

+0

Jestem tak zawstydzony, tak, to powinien być wzrost, ale wciąż mam takie samo efekt, wyszukiwanie maleje po naciśnięciu wewnątrz wyszukiwania, a następnie, jeśli kliknę ponownie, to się zawiesiło.To jest, gdy translatesAutoresizingMaskIntoConstraints = false.Jeśli ustawię go na true, będzie działać poprawnie, ale nie jest wyrównany poprawnie.Mam przerwy między dnem i pasek wyszukiwania: – Kobe

+0

jakikolwiek pomysł, jak to naprawić? – Kobe

Powiązane problemy