How can I change height of Navigation Bar - Swift 3 - ios

I'm pretty sure this is not duplicate because I looked at other answers and they all outdated and do not provide exact answer.
I have Navigation Controller and several view controllers. I want to make Navigation Bar a bit taller so it would fit text size that I need. How can I do that ?
I tried this:
UINavigationBar.appearance().frame = CGRect(x: 0.0, y: 0.0, width: 320.0, height: 210.0)
but nothing happens...
Also, I wasn't able to add any constraints to the Nav Bar using Xcode layout buttons.
Hope somebody can help me to fix this issue. Thanks in advance!

UPDATE 8/3/20: I posted this in 2016. A number of people have stated this no longer works so please use at your own risk. I am not working in iOS at the moment so I do not have an update handy. Best of luck!
Here is one way to do it:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let height: CGFloat = 50 //whatever height you want to add to the existing height
let bounds = self.navigationController!.navigationBar.bounds
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + height)
}

You can write this code in class that extends UINavigationController
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let height = CGFloat(72)
navigationBar.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: height)
}

You can create a class based on UINavigationBar like this
class TTNavigationBar: UINavigationBar {
override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width, height: 55)
}
}
And add it to your NavigationController.

This might be better if your navigation bars in your app all have same adjusted height.
extension UINavigationBar {
open override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width, height: 51)
}
}

you can draw your navigation bar from xib (with your height) and then:
self.navigationController?.navigationBar.addSubview(yourNavBarView)
in alternative, if you want create UIView from code,
let viewNavBar = UIView(frame: CGRect(
origin: CGPoint(x: 0, y:0),
size: CGSize(width: self.view.frame.size.width, height: 100)))
and then adding to nav bar.

Related

How to adjust Subview according to different Screen Sizes (Height and Width)?

I am trying to add a view onto the screen using the following code:-
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let size = CGSize(width: 564.0, height: 783.0)
let host = UIView(frame: CGRect(x: 0.0, y: 0.0, width: size.self.width, height: size.self.height))
self.view.addSubview(host)
let particlesLayer = CAEmitterLayer()
particlesLayer.frame = CGRect(x: 0.0, y: 0.0, width: size.self.width, height: size.self.height)
host.layer.addSublayer(particlesLayer)
host.layer.masksToBounds = true
}
But, the view is not able to cover the entire screen on an iPad, but, is able to cover only the size of an iPhone screen. Could anyone please let me know what can I do to resolve this issue? Thanks a lot for the help!🙏
You can use below to get device's screen size.
let width = UIScreen.main.bounds.width
let height = UIScreen.main.bounds.height
However, you should consider using autolayout. It is more clean and useful compare to your approach.

How to change UINavigationBar height?

Is it possible to change the UINavigationBar height in ios 11?
I've tried this:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let height: CGFloat = 50 //whatever height you want to add to the existing height
let bounds = self.navigationController!.navigationBar.bounds
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + height)
}
and this:
extension UINavigationBar {
override open func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width, height: 100)
}
}
and none of them seem to work.

Height of UINavigationBar in UITableView doesn't change

I have a tabbed application, containing as a first tab "Home", which is the initial view controller. Home is a UITableViewController and conforms to BETableViewController, as all the tabs of the app, and is embeded in a UINavigationViewController.
When you select a row in Home, you get to ArticleViewController, conforming to BEViewController .
BEViewController and BETableViewController have a large (height has been enlarged) UINavigationBar containing a logo; those changes come in the viewDidLoad() function of each Controller.
The problem is that, when i load the app, Home shows a normal sized UINavigationBar with the logo protrudind into the TableView.
. When I select a row, it takes me to ArticleViewController, which does show the wanted results in the navigation bar. Wheni go back to Home, through the back button, Home shows the intended results.
I'll include the screenshots and some code.
class BEViewController: UIViewController {
var logo = UIImageView() //CGSize(width: 100, height: 100))
override func viewDidLoad() {
super.viewDidLoad()
logo.frame = CGRect(x: self.view.frame.width / 2 - 40, y: 20, width: 80, height: 80)
logo.image = #imageLiteral(resourceName: "logo")
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 100)
self.navigationController?.navigationBar.addSubview(logo)
}
}
class BETableViewController: UITableViewController {
var logo = UIImageView() //CGSize(width: 100, height: 100))
override func viewDidLoad() {
super.viewDidLoad()
logo.frame = CGRect(x: self.view.frame.width / 2 - 40, y: 20, width: 80, height: 80)
logo.image = #imageLiteral(resourceName: "logo")
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 100)
self.navigationController?.navigationBar.addSubview(logo)
}
}
Image 1: Initial Home
Image 2: ArticleViewController
Please set the size of image in Navigation Bar less than 49 pt. Please refer Apple Guidelines - Navigation Bar

Inset UITextField, with different values for left and right

I'm trying to do a clone of Instagram and I little bit stuck with a UITextField customization. Maybe is a noob question but here it goes.
What I'm trying to achieve if to have different inset for my UITextField, one smaller on the left side and one bigger on the right side, so I have space enough for the clear button from UITextField.
I'm trying to achieve this:
Instagram app
but the closest thing I get is this:
Clone app
As you can see the clear button is much more squeeze on my version, but I can't handle to inset only the right side a little bit more, without touching the left one.
I used the following code on my UITextField subclass (Swift 3):
override func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: 16, dy: 0)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: 16, dy: 0)
}
But it only let me do it for booth side changing dx. Any suggestion? Thanks in advance
This works for me
let rightView = UIView(frame: CGRectMake(0, 0, 16, 16))
textField.rightViewMode = UITextFieldViewMode.Always
textField.rightView = rightView
let leftView = UIView(frame: CGRectMake(0, 0, 20, 20))
textField.leftViewMode = UITextFieldViewMode.Always
textField.leftView = leftView
May it helps you.
Managed to solve it by myself:
override func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: 16, dy: 0)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: bounds.origin.x + 16, y: bounds.origin.y, width: bounds.width - 56, height: bounds.height)
}
override func clearButtonRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: bounds.width - 36, y: bounds.origin.y, width: 16, height: bounds.height)
}
This way it work just like Instagram. It inset a little bit more the right side when is in editing mode, and when is in normal mode it has a inset of 16 both side.

Setting UICollectionView frame at runtime

I am trying to set the frame of my UICollectionView at runtime.
I tried using
mainCollectionView.frame = CGRect(x: 20, y: 20, width: self.view.frame.width-20, height: self.view.frame.width-20)
in my viewDidLoad but unfortunately it will always stay as in IB.
I am not yet using constraints.
Otherwise resizing my cells is working in cellForItem:
collectionCell.frame.size.width = self.view.frame.width/8 * 3
Try to set frame in viewDidLayoutSubviews
you are wrong write height because you are set width at height.
try this line without navigation bar:
mainCollectionView.frame = CGRect(x: 20, y: 20, width: UIScreen.mainScreen().bounds.size.width - (20 * 2), height: UIScreen.mainScreen().bounds.size.height - (20 * 2))
Run the following in one of the LayoutSubviews overrides. This will tell collection view to update its frame.
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
mainCollectionView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
mainCollectionView.collectionViewLayout.invalidateLayout()
}

Resources