Add UIView in navigation bar - ios

I want to create UINavigationBar with rounded corner. It will look like this
What I am thinking is I add UIView with rounded corner and insert it in navigation bar. So this is my code
let roundView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 44))
roundView.backgroundColor = UIColor.whiteBackground
roundView.roundCorners(corners: [.topLeft, .topRight], radius: 20)
navigationController?.navigationBar.insertSubview(roundView, at: 0)
setTitleTextColor(color: UIColor.black)
By the UI, this works well. But then my UIBarButtonItem is missing, it covered up by my custom view and couldn't be clicked. So my question is, how to add subview in navigation bar?
Thank you!

Just not use UINavigation bar and create all by scratch. Is the easiest way. Otherwise you can try with pattern image:
navigationController?.navigationBar.backgroundColor = UIColor(patternImage: UIImage(named: "background.png"))

From Storyboard,
You have ViewController with navigationController
Select navigationController and deselect the below selected option i.e. Show Navigation Bar visibility.
Take a UIView (purpleView) with constraints
top, leading trailing = 0 w.r.t. superview
height = 64
Take another UIView (whiteView) in purpleView with constraints
top= 20 (for status bar)
leading trailing bottom= 0 w.r.t. purpleView
Now add cancel and label to your whiteview
Now your UI Hierarchy is like below
Take outlet of whiteView and make corner radius
Thats it.
If you'r not using storyboard then you can do same with code also. In this case you have to set frame of purpleView and whiteView instead of constraints.
Hope now its clear to you.

How about to make it as a normal UIView and hide the navBar and show it in the next VC, who will know which trick you have used.
read this short article here

Related

How to properly create an overlay view in storyboard?

My aim is to create a popup view right below a button once is clicked, this is the concept:
The way I prepared the storyboard is by using two different UIViewController. The main one contains the two buttons and, once clicked, I modally push the second UIViewController over the current context.
My question is: what's the best or properly way to create constrain in order to move the "Tapped" image right below the button?
The way I do this is to add a childcontroller to your first storyboard vc. Then adjust the frame of the container view in the storyboard, which I hope will be easier than programmatically adjusting frames.
Then be sure to change the class on top of the VC that Xcode created for you.
When the button is clicked get the the bottom position of it (it's y in frame + height) and send it to the popup , then set them as the top constraint of the imageView in popup in viewDidLayoutSubview
You can calculate the position of the overlay using the button's and the view's positions and dimensions. For example, the overlay's y can be the button's frame.maxY + constant.
let x = ...
let y = ...
let width = ...
let height = ...
let overlay = UIView(frame: CGRect(x: x, y: y, width: width, height: height))
view.addSubView(overlay)
The translucent black view can be added as a UIView with the same frame as the view of the VC and constrained to always be the same as the frame of the VC's view. Add this view first, before you add overlay.

Swift - get size of viewController with navigation bar and tab bar

my viewController has both navigation and tab bars.
I need to figure out available space after subtracting size of navigation and tab bar.
I tried:
let height = UIScreen.main.bounds.height - self.navigationController!.navigationBar.frame.size.height - self.tabBarController!.tabBar.frame.size.height
But it is not working...
Am I doing something wrong?
EDITED:
Sorry lacked information.
I want to embed UIScroll view in between Navigation and Tab bar. The equation returned height that is bigger than available space.
titleView = UIScrollView(frame: CGRect(x: 0, y:0, width: titleWidth, height: height))
The titleView surpassed "tabbar.y"
The solution was to also subtract height of statusBar. Damn..
The easiest way to get this information is using layout guides.
let viewHeight = view.frame.height - (topLayoutGuide.length + bottomLayoutGuide.length)
However, I think you're much better off using Autolayout to manage the height of your scrollView instead of setting in directly.

Moving Tab Bar to the top of the screen swift

I want to have the Tab Bar at the top of the screen. One post suggested to do the followings (I put the following code in the viewDidLoad() of the UITabBarController) :
CODE
let tabBar = self.tabBar
// yStatusBar indicates the height of the status bar
let yStatusBar = UIApplication.sharedApplication().statusBarFrame.size.height
// Set the size and the position in the screen of the tab bar
tabBar.frame = CGRectMake(0, yStatusBar, tabBar.frame.size.width, tabBar.frame.size.height)
There are 2 problems with this solution:
The bottom of the screen is left with a black region where the tab bar was
The Tab bar covers the view at the top of the screen - the constraints of that view is relative to the device but they should be relative to the Tab bar. However when the screen is designed in the IB there is no Tab bar to relate to.
Is there a way to overcome these problems? P.S. I am new to IOS
let tabBar = self.tabBarController?.tabBar
// Set the size and the position in the screen of the tab bar
tabBar?.frame = CGRect(x: 0, y: self.view.frame.height, width: (tabBar?.frame.size.width)!, height: (tabBar?.frame.size.height)!)
Although it is against the human interface guidelines there exist a hack if you really want to.
You could create a blank UIView in your storyboard (with proper constraints set up) that would essentially be the placeholder for the tabBar when loaded.
You then set top constraints for your other views relative to this view that you have setup.
This works, but probably not best practice to do so

UILabel misplaced after pushing to controller and setting hidesBottomBarWhenPushed

Having a bit of an issue with my tab bar app. I want to hide the bottom bar in the next controller I push to. I have set the hidesBottomBarWhenPushed to true in the IB. I have a UILabel pinned to the bottom of the screen in this controller. When I push to it the label is not at the bottom of the screen but above it at the same height the toolbar was. Any ideas what I might be doing wrong here? Any pointers would be great!
Not the cleanest of solutions, but you could rearrange the label when the BottomBar gets hidden
Like this:
label.frame = CGRect(x: label.frame.origin.x, y: label.frame.origin.y + 'tabbarHeight', width: label.frame.width, height: label.frame.height)
You would just have to add the height of your tabBar to the y value
Edit: This is also reversible, so when you are navigating back, just subtract the tabBarHeight from the y value

Avoid status bar transparency while animating

I got the viewController with 2 elements:
view with labels and buttons
tableView
I'm getting the following animation by changing view's height constraint from 170 to 0 and than animating view.layoutIfNeeded() and tableView.layoutIfNeeded().
My goal is to hide menu when content offset of the tableView reaches some value.
This works fine, except I got an overlay of status bar over the moving content from my view. Are there any options to add a sublayer to status bar not to be transparent? Or any other suggestions?
Thanks!
Create a view, put it where the status bar will be, and set its background color to which ever color you require. For example:
let statusBarView = UIView(frame: UIApplication.sharedApplication().statusBarFrame)
statusBarView.backgroundColor = UIColor.blackColor()
view.addSubview(statusBarView)
Or, set the content edge inset by (20.0, 0.0, 0.0, 0.0), which I would agree is a much more elegant solution, as suggested by #holex in the comments

Resources