Showing a label right below navigation bar in iOS - ios

How do I show a label (or any UIView) right below navigation bar in a UIViewController.
I used the following code:
let label = UILabel(frame: CGRectMake(100, (self.navigationController?.navigationBar.frame.height)!, 200, 21))
However, using self.navigationController?.navigationBar.frame.height)! is not sufficient. Using this y co-ordinate keeps label invisible.
What should I use for y co-ordinate to show a UIView right below navigation bar.

Try to:
let label = UILabel(frame: CGRectMake(100, (self.navigationController?.navigationBar.frame.height)!, 200, 21))
y = self.navigationController?.navigationBar.frame.origin.y + self.navigationController?.navigationBar.frame.size.height;
after set y value.

Also consider the status bar height which can be found using
UIApplication.sharedApplication().statusBarFrame.size.height
so it should be
UILabel(frame: CGRectMake(100, ((self.navigationController?.navigationBar.frame.height)! + UIApplication.sharedApplication().statusBarFrame.size.height), 200, 21))

Related

Add UIView in navigation bar

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

Draw custom view within UITextField

I want to extend UITextField to draw inputed text in a custom UILabel like class that draws certain symbols in a special way. Would I have to modify the drawing code directly or is there a way to add this view "on top" of the UITextField while still being able to input text into the UITextField?
This should add a view to a text field. Now just change thisView to any custom view you want with any custom frame you want and pow! You got what you want ;)
let frame = CGRect(x: 0, y: 0, width: 30, height: 30)
let thisView = UIView(frame: frame)
thisView.backgroundColor = UIColor.blue
textField.addSubview(thisView)

Swift Nav Bar with Title and Page Control

I am developing a swift app but having some problem on putting title and page control, together on the nav bar. I only manage to put either one of them on the nav bar, but not both. I am hoping for something like this(as well as the animation when swiping to another view controller) :
Below is my screen output. Page controller(embedded in Navigation controller) is working, just wanted to add titles(change base on view) and page control.
And here's how I create the title:
This is how I create the title:
#IBOutlet weak var navBar: UINavigationItem!
let title: UILabel = UILabel(frame: CGRectMake(0, 0, 150, 44))
title.numberOfLines = 2
title.textAlignment = .Center
title.text = "News\n"
navBar.titleView = title
I am making the title into 2 lines to leave room for the page control. However, when I try to create page control programmatically, it appears to be "behind" the nav bar.
This is how create page control:
let pageControl : UIPageControl = UIPageControl(frame: CGRectMake(0, 0, 150, 44))
self.pageControl.numberOfPages = 3
self.pageControl.currentPage = 0
self.view.addSubview(pageControl)
Add both your label and page control to another view, setting the constraints / frames appropriately, then add that view as the titleView for the nav bar.

How to increase the height of navigation bar in Xcode?

I'm working on a app, in which I need to keep a navigation bar.
when I write any title on the bar, the time and the title kinda get very close to each other. I wanted to increase the height of the bar, so it can get some breathing room.
select your ViewController --> select your Navigation Item --> Prompt --> Add space it increase the height of **Navigation bar**
Check Image here :
Programatically
Add this in viewWillAppear or viewDidAppear method
Objective-C
[self.navigationController.navigationBar setFrame:CGRectMake(0, 0, self.view.frame.size.width,80.0)];
Swift
self.navigationController.navigationBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 80.0)
Swift-3
self.navigationController!.navigationBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 80.0)
iOS 11
objective C
for (UIView *subview in self.navigationController.navigationBar.subviews) {
if ([NSStringFromClass([subview class]) containsString:#"BarBackground"]) {
CGRect subViewFrame = subview.frame;
// subViewFrame.origin.y = -20;
subViewFrame.size.height = 100;
[subview setFrame: subViewFrame];
}
}
swift
for subview in (self.navigationController?.navigationBar.subviews)! {
if NSStringFromClass(subview.classForCoder).contains("BarBackground") {
var subViewFrame: CGRect = subview.frame
// subViewFrame.origin.y = -20;
subViewFrame.size.height = 100
subview.frame = subViewFrame
}
}
simply add this line to your viewController
navigationController?.additionalSafeAreaInsets.top = 30
// where 30 is the extra space, add as per your need.
Apple proposes not to resize navigationBar itself, but remove shadow from bar and add custom view under your navigationBar. This can work for most cases. Check Apple's samples.
Add the following extension to your project:
import UIKit
extension UINavigationBar {
override open func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: UIScreen.main.bounds.size.width, height: 80.0)
}
}
Please refer the apple recommended approach for extended navigation bar here,
https://developer.apple.com/library/content/samplecode/NavBar/Introduction/Intro.html
THIS SOLUTION NO LONGER WORKS IN Xcode 8.x.x and later!
you can also increase height without creating the custom navigation follow the following steps
Step 1 Selecte Navigation bar in Storyboard or XIB
Step 2 Copy ObjectID from Identity Inspector
Step 3 Open Storyboard/XIB as Source Code
Step 4 Find ObjectID in Source Code past ObjectID in search
Step 5 Edit height! thats all
I hope this will help you
Add this in viewWillAppear method
CGFloat height = 80;
[self.navigationController.navigationBar setFrame:CGRectMake(0, 0,
self.view.frame.size.width,height)];
if it increase first and shrinks to original height then add this code in viewDidAppear method
We need to change the height of the navigation bar for each time the view show.So put the code on viewWillAppear
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 80)
}
we can set the width as the width of the view and change the height as we wish.
You can't change the height of the default NavigationBar if I'm not wrong.
Although, you can create a custom NavigationBar and add a custom height to it.
navigationController?.additionalSafeAreaInsets.top = 25
Add this to viewDidLoad. it will definitely work. Successfully worked in Xcode 12-version
[self.navigationController.navigationBar setPrefersLargeTitles:YES];
is going to Increase the Navigation bar height Programmatically

How to make UITextField appear with Swift Code

I want to make the following UITextField appear when I compile and run the application. I have the UITextField declared and initialised as:
var myTextField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200.00, height: 40.00));
However when I run the app, this text field does not appear. What Swift code would make the textfield appear?
You have to actually add the text field to the view hierarchy, which is done slightly differently depending on the class that you're in. If for example, you're in a UIViewController subclass, you add it as a subview of the UIView attached to the view controller's view property.
var myTextField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200.00, height: 40.00));
self.view.addSubview(myTextField)
Or, if you're working with a subclass of UIView, you could use:
self.addSubview(myTextField)
After you've added your text field to the view hierarchy, it's still possible that you won't be able to see it. By default, text field will have a white background color, no text, and no border around it. This can all be changed through use of the following properties.
myTextField.backgroundColor = UIColor.redColor()
myTextField.text = "some string"
myTextField.borderStyle = UITextBorderStyle.Line
You need to add this UITextField as a subview of another UIView.
Assuming you are inside a UIViewController, you could call self.view.addSubview(myTextField).

Resources