Move tableview below top tab bar - uitableview

I want to move my table view below tabbar. Presently first row of the tableview is getting hidden behind the tab bar.
I tried
override func viewDidAppear(animated: Bool)
{
self.tableView.frame.origin.y = 50
print("done")
}
I tried the same in viewDidLoad and viewWilAppear
Kindly help me with this
Screen Shot of TabBar with TablView

Try putting this in your ViewDidLoad()method in your view controller. It will move the TableViewController to begin after the navigation bar. However, you will also need to subtract the status bar that is above your navigation bar so I have also added that for you.
self.tableView.contentInset = UIEdgeInsetsMake(-self.navigationController!.navigationBar.frame.size.height - 20, 0, 0, 0)
Cheers.

Related

UISearchBar Disappear From UINavigationBar When Use "back swipe" iOS 12

I have 2 ViewController for both implemented:
let searchBar = UISearchBar()
func viewDidLoad() {
navigationItem.titleView = searchBar
}
When I push second view controller and try go back with swipe gesture, all my navigation items disappearing irrevocably.
Maybe someone has an idea how to fix it?
Images:
SearchBar First VC
https://imgur.com/QJxflWP.png
SearchBar Second VC
https://imgur.com/FUBo0t6.png
NavigationBar When starting back swipe
https://imgur.com/G2FXrnq.png
A navigation item is associated with the view controller. So if you want search bar in both view controllers you have to set the search bar for both the view controllers.
Also, you can use searchController property to show the search bar in the navigation controller. So that you can show the title and search bar in the navigation bar.
A navigation item is associated with the view controller. So if you want to show search bar in both view controllers you have to add it separately in the viewDidLoad method.
And also you can use searchController property to show the search bar in the navigation bar. So that you can show both title and search bar in the navigation bar.
I finally solve this problem by handling UINavigationBarDelegate methods.
Use your own UINavigationController because UINavigationController is the default delegate of UINavigationBar, i don't need to reassign delegate to other class to handle.
implement these code and get the searchField inside of UISearchBar to minor change its frame.
extension YourNavigationController: UINavigationBarDelegate {
func navigationBar(_ navigationBar: UINavigationBar, shouldPop item:
UINavigationItem) -> Bool {
if let inSearchBar = item.titleView as? UISearchBar,
let searchField = inSearchBar.value(forKey: "searchField") as? UIView {
searchField.frame = CGRect(x: 0, y: 0, width: searchField.frame.width + 1, height: 36)
}
}
}
Why i do this is because i found that if the frame of searchField don't change, then the search bar would disappear. It is not a good way to do but a kind of workaround to show the correct view. In iOS 13, i believe we can check to use searchTextField to replace searchField, a property provided by Apple doc.

How can I prevent the child view being cut off when I add a tab bar controller?

I want the green view to move forward from the container view as follows.
However, when I add a tab bar controller, the green view is cut off as follows.
I tried the following codes so that the green view is not cut off. But it did not work.
containerView.clipsToBounds = false
containerView.layer.zPosition = 100
self.view.bringSubview(toFront: containerView)
The problem seems to be not in the container view. Because when tab bar controller was added, green view started to be cut off.
When I add a tab bar controller, how can I prevent the green view from being cut off?
The problem is that UITransitionView in your UITabBarController clips all subviews. You can fix this easily if you remove clipsSubview from every subview in your TabBarController. I make this with custom TabBarController. Here is my code
class CustomTabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
for item in self.view.subviews{
item.clipsToBounds = false
}
self.view.clipsToBounds = false
}
}

Force the navigation bar to add space for the status bar

I have a UIPageViewController subclass that allows the user to swipe upward from the initial view controller to a second view controller. The initial view controller does not have a navigation bar (it's not connected to a UINavigationController), but the second one does. Currently, when I swipe up to present the second view controller, there is no space in the navigation bar for the status bar. When the transition is complete, the entire view controller layout updates instantly to add the status bar space. I want to make the second view controller anticipate that the status bar will be there and add the space for it so the view doesn't jolt after the transition.
Short video clip showing the jolt
Similar, but unanswered question
EDIT:
After closely looking at the way the view controller behaves during the transition, I was able to get a good look at what is going on here.
When I swipe up, the second view controller moves upward and aligns the top edge of the navigation bar (no status bar) with the top of the screen. It appears that the size/layout of the content is correct, so there's whitespace at the bottom that's the height of the status bar. See the screenshot:
When the transition is finished, space for the status bar is added and the view gets shifted downward.
I found a workaround that seems to work pretty well. I need to test this with the iPhone X to see if I need to make adjustments. If someone finds a better solution, I'll make it the accepted answer.
I basically shift my page view controller down by 20pt when it is loaded:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let f = view.frame
view.frame = CGRect(x: 0, y: 20, width: f.size.width, height: f.size.height)
view.setNeedsLayout()
view.layoutIfNeeded()
}
After the transition, I shift the page view controller back up to its original position at exactly the same time that the second view controller adds status bar space. This causes the second view controller to slide right below the status bar, where it should be. There's no noticeable jolt. I used this in my page view controller's delegate:
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
guard previousViewControllers.count > 0 else { return }
// if the transition completed and the view controller we transitioned from is the first view controller
if completed && viewControllers!.index(of: previousViewControllers.first!) == 0 {
let f = view.frame
view.frame = CGRect(x: 0, y: 0, width: f.size.width, height: f.size.height)
view.setNeedsLayout()
view.layoutIfNeeded()
}
}

How to add constraints to TableView on TableViewController?

I have TabBar, SearchController and TableView...
To fix the problem with the black screen, I added following string to my TableViewController that places SearchController on itself:
self.definesPresentationContext = true
But, I receive next problem - after I move to the next controller and back to the TableViewController table is placed below the navigation bar.
How can I fix that?
You can try this in your tableviewController viewDidLoad method:
yourTableView.contentInset = UIEdgeInsets(top: 50, left: 0, bottom: 0, right: 0)
select your viewcontroller and uncheck the under top bars i think its help for you
Select your ViewController and Select the None in under top bars.
And you need to update the code in ViewController
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.isNavigationBarHidden=true
}

hide NavigationBar without lifting up the View

hiding my NavigationBar with this :
func hideAndShow(){
if self.navigationController?.navigationBar.hidden == true {
self.navigationController?.setNavigationBarHidden(false, animated: true)
}else {
self.navigationController?.setNavigationBarHidden(true, animated: true)
}
}
but its also lifting up my View (maybe because View is below the Navigation), how can i hide it without lifting up my View
see the below image for better understanding
as you can see that my image in my view also get shifted up while hiding the NavigationBar any idea how can i fix this ??
can we just show the view below the layer of navigation bar ???
Two steps are required to solve your problem:
Add self.edgesForExtendedLayout = UIRectEdge.All to viewWillAppear. As a result your view will start at the top of the screen instead of below the NavigationBar. You can read more about edgesForExtendedLayout here: https://stackoverflow.com/a/19585104/1447641
Add a top constraint of {navigationbarheight} to your ImageView.
After that the position of the ImageView shouldn't be effected by the NavigationBar anymore.

Resources