Preserve UITabBar when pushing a ViewController from a UITableView - ios

I'm trying to push a View Controller from a TableView displayed inside a UiViewController that is included in a UITabBarController (it's more simple that it looks...)
The View Controller is pushed BUT the UITabBar is hidden by this view controller, and I'd like to keep this UITabBar displayed.
I tried a lot of things, unsuccessfully.
Here is my code:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let v = UIViewController()
v.view=UIView(frame: CGRectMake(0,0,self.view.frame.size.width,200))
v.view.backgroundColor=UIColor.redColor()
v.hidesBottomBarWhenPushed = false
self.tabBarController?.navigationController?.hidesBottomBarWhenPushed = false
self.tabBarController?.navigationController?.pushViewController(v, animated: true)
}
Any hint will be really appreciated.
Regards,

Related

Custom Segmented Control (UIView) In Front of Pushed View

Hi I have implemented a custom animated push when my collection View Cell is tapped. The issue I'm having is that when this view controller is pushed forward, my custom segmented controller is on top of it still. Here is my code for my push:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = PopUpCellViewController(nibName: "PopUpCellViewController", bundle: nil)
let cell = collectionView.cellForItem(at: indexPath)
sourceCell = cell
self.navigationController?.pushViewController(vc, animated: true)
}
Let me know if you can help me put this Segmented control below my "PopUpCellViewController" when it is called.
You are probably adding your custom segmented controller as a subview to the navigation bar. Never do that: you must not add any subviews to a navigation bar. Instead, make the segmented controller your view controller's navigationItem.titleView. It will then occupy the center of the navigation bar, but only in that one view controller (not after the push).

Deselect row of a UITableViewController's UITableView when embedded in a PageViewController

Normally when a UITableViewController that is embedded in a UINavigationController, is returned to from it's detail screen the previously selected row's highlight fades away.
I am trying to replicate this behaviour when a UITableViewController is loaded by a UIPageViewController, but have been unsuccessful so far.
To be clear, my hierarchy when in the detail screen is: UINavigationController>UIPageViewController>UITableViewController>DetailController
Putting the deselectRow:atIndexPath: table view method in the viewDidAppear of the UITableViewController as viewDidAppear is not called. Putting it in the same method, but of the UIPageViewController works, but there is a noticeable delay before the selection fades away.
Has anyone managed to get this to work properly?
Are you referring to UITableView or UITableViewController here
I am trying to replicate this behaviour when a
UITableViewController is loaded by a UINavigationController, but have been unsuccessful so far.
If its a UITableViewController, then it has this clearsSelectionOnViewWillAppear property which does it for you. And it defaults to true.
Not an ideal solution, but this works for me...
Table View Controller code changes:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tableView.reloadData() //added this
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "appCell") as! AppTableViewCell
cell.app = apps[indexPath.row]
cell.delegate = self
cell.reset() //added this
return cell
}
Table View Cell code changes:
//added this method
func reset() {
shortcutTypesTableView.reloadData()
}
Basically, I'm reloading the whole top table view, and in tableView(_:cellForRowAt:) I'm telling each cell to reset, which force's each cell's table view to also reload.
My app doesn't have much overhead when loading this table view (at least yet) so I don't notice a performance difference. However, I would not call this solution "ideal" for performance.

UITableViewController cell remains highlighted when segueing back

I have a UITableViewController and when a cell is selected, my app segues to a new view controller. When I press the back button in my navigation controller to go back to the previous tableview controller, the cell that I had selected remains highlighted. I checked clearsSelectionOnViewWillAppear in viewWillAppeaer and it is true. Any idea why this is happening?
Are you calling the segue from didSelectRowAtIndexPath if so try something like this
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("Your Segue", sender: indexPath)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
You could also place tableView.deselectRowAtIndexPath the in the prepareForSegue method which is what I tend to do

How to push to a different UITableView within the same ViewController and navigate back to first UITableView?

I have a Tab Bar Controller setup. When the user selects a tab, it takes them to a UIViewController. Currently I have a UITableView setup in this View Controller. What I would like to do is, upon selecting a cell from TableView1, I'd like to have a "push" effect where TableView2 comes in from the right side of the screen, and take over.
I've found the following question, similar to mine:
UITableView segue within the same ViewController
One of the suggestion is exactly what I want to achieve, however I could not get it to have that slide effect.
What I've done is added two UITableViews, then tableView1.hidden = true in viewDidLoad.
Then in code:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell: UITableViewCell!
if tableView == tableView1
{
// Dequeue the cell to load data
cell = tableView.dequeueReusableCellWithIdentifier("ID1", forIndexPath: indexPath)
....
}
else if tableView == tableView2
{
// Dequeue the cell to load data
cell = tableView.dequeueReusableCellWithIdentifier("ID2", forIndexPath: indexPath)
....
}
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
if tableView == tableView1
{
tableView2.hidden = false
tableView1.hidden = true
playlistVideosTableView.reloadData()
// Deselects the row
tableView1.deselectRowAtIndexPath(indexPath, animated: true)
}
}
However, it doesn't have that "slide effect" and makes the tableview appears instant.
I could make another UIViewController with the second tableview but I don't want to make my Tab Bar disappear.
How can I achieve this, and how can I ensure the ensure gets back to the first UITableView?
Thanks
I would recommend to simply create another UIViewController that has a UITableView, so preferably a UITableViewController.
Then in tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) you call a segue to the new ViewController. This will give you the desired effect and is a cleaner solution as well.
You will not lose your TabBar either with this solution if you embed all ViewControllers in this tab into a NavigationController.
EDIT: sample code
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("editSpecialStage", sender: tableView.cellForRowAtIndexPath(indexPath))
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let indexPath = self.tableView.indexPathForCell(sender as! UITableViewCell)!
let secondViewController = segue.destinationViewController as! SecondViewController
}
And in the Storyboard you have a NavigationController as the first ViewController from the TabBar. Then the first ViewController and then the second, connected via push segue.
I could make another UIViewController with the second tableview but I don't want to make my Tab Bar disappear.
Why will your tab bar disappear? It wouldn't if you used a UINavigationController!
Just do what I say, alright?
First create that new view controller and connect the two VCs with a show segue
First VC -show-> Second VC
Now the whole picture would look like
Tab Bar VC -view controllers-> First VC -show-> Second VC
Now modify this to
Tab Bar VC -view controllers-> Navigation Controller -root vc-> First VC -show-> Second VC
Now the segue will slide the Second VC to the left and your tab bar won't disappear!

Swift - Open ViewController on Tapping a TableViewCell

I have a tableView with identical cells. After selecting one of them I would like to open another ViewController.
I tried this code:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
let destination = EditViewController()
navigationController?.pushViewController(destination, animated: true)
}
but it didn't open another ViewController.
Can You give me a hint?
Just go inside you storyboard, Select you initiate view controller, Then click on Editor > Embedded In and select Navigation controller, Then Your code will be work. This mean you have set the navigation stack for your view controllers from initial view controller.
Most probably you just don't have the navigationController there - use rather present to show the viewController:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
let destination = EditViewController()
self.present(destination, animated: true, completion: nil)
}
Or if you want the viewController to be pushed, make sure that you embed the tableViewController into a UINavigationController.

Resources