Connect UISearchController to UISearchBar without embedding the search bar in the tableview or in the...
First of all, sorry if this question sounds too stupid (feel free to skip it).
In my app, I need to create a search area: basically, I should create a search bar and, when the user taps some characters in, I show a tableview with the results.
In my main ViewController I did the following:
var resultSearchController:UISearchController? = nil
override func viewDidLoad() {
super.viewDidLoad()
// my results table is 'SearchTable' in my storyboard
let searchTable = storyboard!.instantiateViewController(withIdentifier: "SearchTable") as! SearchTable
resultSearchController = UISearchController(searchResultsController: searchTable)
resultSearchController?.searchResultsUpdater = searchTable as? UISearchResultsUpdating
resultSearchController?.hidesNavigationBarDuringPresentation = false
resultSearchController?.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
Of course, anything works fine if I add the following:
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search..."
// this line embeds the search bar in the navigation bar
navigationItem.titleView = resultSearchController?.searchBar
The UISearchController creates a UISearchBar and sets the search controller's searchBar property to it.
Problem is I want a different thing, that is:
- the search bar should not be embedded in the navigation bar;
- the search bar should not be embedded in the tableview itself;
My search bar should be always visible, fixed and placed right below the navigation bar: I simply put an UISearchBar in my storyboard, with its constraints and connected it to the viewcontroller as an IBOutlet.
But, at this point I don't know how to relate my searchbar to the resultSearchController. How could I do? Is it possible?
ios swift search uisearchbar uisearchcontroller
add a comment |
First of all, sorry if this question sounds too stupid (feel free to skip it).
In my app, I need to create a search area: basically, I should create a search bar and, when the user taps some characters in, I show a tableview with the results.
In my main ViewController I did the following:
var resultSearchController:UISearchController? = nil
override func viewDidLoad() {
super.viewDidLoad()
// my results table is 'SearchTable' in my storyboard
let searchTable = storyboard!.instantiateViewController(withIdentifier: "SearchTable") as! SearchTable
resultSearchController = UISearchController(searchResultsController: searchTable)
resultSearchController?.searchResultsUpdater = searchTable as? UISearchResultsUpdating
resultSearchController?.hidesNavigationBarDuringPresentation = false
resultSearchController?.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
Of course, anything works fine if I add the following:
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search..."
// this line embeds the search bar in the navigation bar
navigationItem.titleView = resultSearchController?.searchBar
The UISearchController creates a UISearchBar and sets the search controller's searchBar property to it.
Problem is I want a different thing, that is:
- the search bar should not be embedded in the navigation bar;
- the search bar should not be embedded in the tableview itself;
My search bar should be always visible, fixed and placed right below the navigation bar: I simply put an UISearchBar in my storyboard, with its constraints and connected it to the viewcontroller as an IBOutlet.
But, at this point I don't know how to relate my searchbar to the resultSearchController. How could I do? Is it possible?
ios swift search uisearchbar uisearchcontroller
What’s your minimum deployment target? (What lowest version of iOS do you need to support)
– Cabus
Jan 20 at 9:42
I can even support the latest, there's no minimum
– user5273262
Jan 20 at 10:07
No. Your minimum target is in your Xcode settings of your project. Did you select 11? 12? 10? Cause using searchController is quite different in every iOS versions.
– Glenn
Jan 20 at 11:35
Glenn, it currently is 12.0 (sorry, I ignored there were those differences)
– user5273262
Jan 20 at 11:37
add a comment |
First of all, sorry if this question sounds too stupid (feel free to skip it).
In my app, I need to create a search area: basically, I should create a search bar and, when the user taps some characters in, I show a tableview with the results.
In my main ViewController I did the following:
var resultSearchController:UISearchController? = nil
override func viewDidLoad() {
super.viewDidLoad()
// my results table is 'SearchTable' in my storyboard
let searchTable = storyboard!.instantiateViewController(withIdentifier: "SearchTable") as! SearchTable
resultSearchController = UISearchController(searchResultsController: searchTable)
resultSearchController?.searchResultsUpdater = searchTable as? UISearchResultsUpdating
resultSearchController?.hidesNavigationBarDuringPresentation = false
resultSearchController?.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
Of course, anything works fine if I add the following:
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search..."
// this line embeds the search bar in the navigation bar
navigationItem.titleView = resultSearchController?.searchBar
The UISearchController creates a UISearchBar and sets the search controller's searchBar property to it.
Problem is I want a different thing, that is:
- the search bar should not be embedded in the navigation bar;
- the search bar should not be embedded in the tableview itself;
My search bar should be always visible, fixed and placed right below the navigation bar: I simply put an UISearchBar in my storyboard, with its constraints and connected it to the viewcontroller as an IBOutlet.
But, at this point I don't know how to relate my searchbar to the resultSearchController. How could I do? Is it possible?
ios swift search uisearchbar uisearchcontroller
First of all, sorry if this question sounds too stupid (feel free to skip it).
In my app, I need to create a search area: basically, I should create a search bar and, when the user taps some characters in, I show a tableview with the results.
In my main ViewController I did the following:
var resultSearchController:UISearchController? = nil
override func viewDidLoad() {
super.viewDidLoad()
// my results table is 'SearchTable' in my storyboard
let searchTable = storyboard!.instantiateViewController(withIdentifier: "SearchTable") as! SearchTable
resultSearchController = UISearchController(searchResultsController: searchTable)
resultSearchController?.searchResultsUpdater = searchTable as? UISearchResultsUpdating
resultSearchController?.hidesNavigationBarDuringPresentation = false
resultSearchController?.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
Of course, anything works fine if I add the following:
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search..."
// this line embeds the search bar in the navigation bar
navigationItem.titleView = resultSearchController?.searchBar
The UISearchController creates a UISearchBar and sets the search controller's searchBar property to it.
Problem is I want a different thing, that is:
- the search bar should not be embedded in the navigation bar;
- the search bar should not be embedded in the tableview itself;
My search bar should be always visible, fixed and placed right below the navigation bar: I simply put an UISearchBar in my storyboard, with its constraints and connected it to the viewcontroller as an IBOutlet.
But, at this point I don't know how to relate my searchbar to the resultSearchController. How could I do? Is it possible?
ios swift search uisearchbar uisearchcontroller
ios swift search uisearchbar uisearchcontroller
edited Jan 20 at 10:26
asked Jan 20 at 9:37
user5273262
What’s your minimum deployment target? (What lowest version of iOS do you need to support)
– Cabus
Jan 20 at 9:42
I can even support the latest, there's no minimum
– user5273262
Jan 20 at 10:07
No. Your minimum target is in your Xcode settings of your project. Did you select 11? 12? 10? Cause using searchController is quite different in every iOS versions.
– Glenn
Jan 20 at 11:35
Glenn, it currently is 12.0 (sorry, I ignored there were those differences)
– user5273262
Jan 20 at 11:37
add a comment |
What’s your minimum deployment target? (What lowest version of iOS do you need to support)
– Cabus
Jan 20 at 9:42
I can even support the latest, there's no minimum
– user5273262
Jan 20 at 10:07
No. Your minimum target is in your Xcode settings of your project. Did you select 11? 12? 10? Cause using searchController is quite different in every iOS versions.
– Glenn
Jan 20 at 11:35
Glenn, it currently is 12.0 (sorry, I ignored there were those differences)
– user5273262
Jan 20 at 11:37
What’s your minimum deployment target? (What lowest version of iOS do you need to support)
– Cabus
Jan 20 at 9:42
What’s your minimum deployment target? (What lowest version of iOS do you need to support)
– Cabus
Jan 20 at 9:42
I can even support the latest, there's no minimum
– user5273262
Jan 20 at 10:07
I can even support the latest, there's no minimum
– user5273262
Jan 20 at 10:07
No. Your minimum target is in your Xcode settings of your project. Did you select 11? 12? 10? Cause using searchController is quite different in every iOS versions.
– Glenn
Jan 20 at 11:35
No. Your minimum target is in your Xcode settings of your project. Did you select 11? 12? 10? Cause using searchController is quite different in every iOS versions.
– Glenn
Jan 20 at 11:35
Glenn, it currently is 12.0 (sorry, I ignored there were those differences)
– user5273262
Jan 20 at 11:37
Glenn, it currently is 12.0 (sorry, I ignored there were those differences)
– user5273262
Jan 20 at 11:37
add a comment |
1 Answer
1
active
oldest
votes
You did all the hard work and the thing that you miss is fairly trivial to what you did.
Basically you should add the UISearchBar resultSearchController!.searchBar as the subview to the view using the code. I don't think it would be possible with Storyboard, that could be what confused you.
I couldn't make it work: I can create a UISearchBar in code, I can add it as a subview, but it is completely unrelated to its context. My goal is replacing the default search bar with mine
– user5273262
Jan 20 at 13:28
I suggested that you should create UISearchController and then its UISearchBar to the view, it seems from your comment that you did something different.
– Ivan Ičin
Jan 20 at 13:34
I'm not understanding: you want to add the searchbar as a subview of the uisearchcontroller? If so, I've not done what you asked
– user5273262
Jan 20 at 14:00
I want you to add resultSearchController!.searchBar to the main view. Not a new UISearchBar if that's what you did, at least that's what you wrote above.
– Ivan Ičin
Jan 20 at 14:12
Also of note what you might be missing and isn't clear from your code is that your UIViewController cannot be UITableViewController, but rather it must be just UIViewController and you must create and add UITableView from the code or from the Storyboard.
– Ivan Ičin
Jan 20 at 14:18
|
show 5 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54275166%2fconnect-uisearchcontroller-to-uisearchbar-without-embedding-the-search-bar-in-th%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You did all the hard work and the thing that you miss is fairly trivial to what you did.
Basically you should add the UISearchBar resultSearchController!.searchBar as the subview to the view using the code. I don't think it would be possible with Storyboard, that could be what confused you.
I couldn't make it work: I can create a UISearchBar in code, I can add it as a subview, but it is completely unrelated to its context. My goal is replacing the default search bar with mine
– user5273262
Jan 20 at 13:28
I suggested that you should create UISearchController and then its UISearchBar to the view, it seems from your comment that you did something different.
– Ivan Ičin
Jan 20 at 13:34
I'm not understanding: you want to add the searchbar as a subview of the uisearchcontroller? If so, I've not done what you asked
– user5273262
Jan 20 at 14:00
I want you to add resultSearchController!.searchBar to the main view. Not a new UISearchBar if that's what you did, at least that's what you wrote above.
– Ivan Ičin
Jan 20 at 14:12
Also of note what you might be missing and isn't clear from your code is that your UIViewController cannot be UITableViewController, but rather it must be just UIViewController and you must create and add UITableView from the code or from the Storyboard.
– Ivan Ičin
Jan 20 at 14:18
|
show 5 more comments
You did all the hard work and the thing that you miss is fairly trivial to what you did.
Basically you should add the UISearchBar resultSearchController!.searchBar as the subview to the view using the code. I don't think it would be possible with Storyboard, that could be what confused you.
I couldn't make it work: I can create a UISearchBar in code, I can add it as a subview, but it is completely unrelated to its context. My goal is replacing the default search bar with mine
– user5273262
Jan 20 at 13:28
I suggested that you should create UISearchController and then its UISearchBar to the view, it seems from your comment that you did something different.
– Ivan Ičin
Jan 20 at 13:34
I'm not understanding: you want to add the searchbar as a subview of the uisearchcontroller? If so, I've not done what you asked
– user5273262
Jan 20 at 14:00
I want you to add resultSearchController!.searchBar to the main view. Not a new UISearchBar if that's what you did, at least that's what you wrote above.
– Ivan Ičin
Jan 20 at 14:12
Also of note what you might be missing and isn't clear from your code is that your UIViewController cannot be UITableViewController, but rather it must be just UIViewController and you must create and add UITableView from the code or from the Storyboard.
– Ivan Ičin
Jan 20 at 14:18
|
show 5 more comments
You did all the hard work and the thing that you miss is fairly trivial to what you did.
Basically you should add the UISearchBar resultSearchController!.searchBar as the subview to the view using the code. I don't think it would be possible with Storyboard, that could be what confused you.
You did all the hard work and the thing that you miss is fairly trivial to what you did.
Basically you should add the UISearchBar resultSearchController!.searchBar as the subview to the view using the code. I don't think it would be possible with Storyboard, that could be what confused you.
edited Jan 20 at 19:06
answered Jan 20 at 12:50
Ivan IčinIvan Ičin
4,02842444
4,02842444
I couldn't make it work: I can create a UISearchBar in code, I can add it as a subview, but it is completely unrelated to its context. My goal is replacing the default search bar with mine
– user5273262
Jan 20 at 13:28
I suggested that you should create UISearchController and then its UISearchBar to the view, it seems from your comment that you did something different.
– Ivan Ičin
Jan 20 at 13:34
I'm not understanding: you want to add the searchbar as a subview of the uisearchcontroller? If so, I've not done what you asked
– user5273262
Jan 20 at 14:00
I want you to add resultSearchController!.searchBar to the main view. Not a new UISearchBar if that's what you did, at least that's what you wrote above.
– Ivan Ičin
Jan 20 at 14:12
Also of note what you might be missing and isn't clear from your code is that your UIViewController cannot be UITableViewController, but rather it must be just UIViewController and you must create and add UITableView from the code or from the Storyboard.
– Ivan Ičin
Jan 20 at 14:18
|
show 5 more comments
I couldn't make it work: I can create a UISearchBar in code, I can add it as a subview, but it is completely unrelated to its context. My goal is replacing the default search bar with mine
– user5273262
Jan 20 at 13:28
I suggested that you should create UISearchController and then its UISearchBar to the view, it seems from your comment that you did something different.
– Ivan Ičin
Jan 20 at 13:34
I'm not understanding: you want to add the searchbar as a subview of the uisearchcontroller? If so, I've not done what you asked
– user5273262
Jan 20 at 14:00
I want you to add resultSearchController!.searchBar to the main view. Not a new UISearchBar if that's what you did, at least that's what you wrote above.
– Ivan Ičin
Jan 20 at 14:12
Also of note what you might be missing and isn't clear from your code is that your UIViewController cannot be UITableViewController, but rather it must be just UIViewController and you must create and add UITableView from the code or from the Storyboard.
– Ivan Ičin
Jan 20 at 14:18
I couldn't make it work: I can create a UISearchBar in code, I can add it as a subview, but it is completely unrelated to its context. My goal is replacing the default search bar with mine
– user5273262
Jan 20 at 13:28
I couldn't make it work: I can create a UISearchBar in code, I can add it as a subview, but it is completely unrelated to its context. My goal is replacing the default search bar with mine
– user5273262
Jan 20 at 13:28
I suggested that you should create UISearchController and then its UISearchBar to the view, it seems from your comment that you did something different.
– Ivan Ičin
Jan 20 at 13:34
I suggested that you should create UISearchController and then its UISearchBar to the view, it seems from your comment that you did something different.
– Ivan Ičin
Jan 20 at 13:34
I'm not understanding: you want to add the searchbar as a subview of the uisearchcontroller? If so, I've not done what you asked
– user5273262
Jan 20 at 14:00
I'm not understanding: you want to add the searchbar as a subview of the uisearchcontroller? If so, I've not done what you asked
– user5273262
Jan 20 at 14:00
I want you to add resultSearchController!.searchBar to the main view. Not a new UISearchBar if that's what you did, at least that's what you wrote above.
– Ivan Ičin
Jan 20 at 14:12
I want you to add resultSearchController!.searchBar to the main view. Not a new UISearchBar if that's what you did, at least that's what you wrote above.
– Ivan Ičin
Jan 20 at 14:12
Also of note what you might be missing and isn't clear from your code is that your UIViewController cannot be UITableViewController, but rather it must be just UIViewController and you must create and add UITableView from the code or from the Storyboard.
– Ivan Ičin
Jan 20 at 14:18
Also of note what you might be missing and isn't clear from your code is that your UIViewController cannot be UITableViewController, but rather it must be just UIViewController and you must create and add UITableView from the code or from the Storyboard.
– Ivan Ičin
Jan 20 at 14:18
|
show 5 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54275166%2fconnect-uisearchcontroller-to-uisearchbar-without-embedding-the-search-bar-in-th%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What’s your minimum deployment target? (What lowest version of iOS do you need to support)
– Cabus
Jan 20 at 9:42
I can even support the latest, there's no minimum
– user5273262
Jan 20 at 10:07
No. Your minimum target is in your Xcode settings of your project. Did you select 11? 12? 10? Cause using searchController is quite different in every iOS versions.
– Glenn
Jan 20 at 11:35
Glenn, it currently is 12.0 (sorry, I ignored there were those differences)
– user5273262
Jan 20 at 11:37