Change height of navigation bar ios - ios

I am trying to change the height of the navigation bar with this code provided in few links over stackoverflow, but it is not working as required.
self.navigationController?.navigationBar.frame = CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y, width: self.view.frame.size.width, height: 200)
self.navigationController?.navigationBar.layoutIfNeeded()
Is there anything additional that I need to add to make it work ?
I did checked the reference link too but it is not working, also it is the same as I written with added fix.

The hierarchy of UINavigationBar view is private so we can't manipulate it.We create our custom view and add into a Viewcontroller.

Related

UITextField as title view in UITableViewControllers embedded in a UIPageViewController

I have a UIPageViewController whose pages are a bunch of UITableViewController. I want to use a UITextField as the title field for each of my table view.
In my UIPageViewController's viewDidLoad, I have the following:
let titleField = UITextField(frame: CGRect(x: 0, y: 0, width: 150, height: 21))
titleField.delegate = self
titleField.placeholder = "(table name)"
navigationItem.titleView = titleField
This works and the text field shows up, but I'd like each of the page (i.e UITableViewController) to handle its own title view. So I moved the above code into my UITableViewController's viewDidLoad, but now nothing shows up.. Why?
Any input would be great. I guess I could keep track of which page I'm currently in in my UIPageViewController but I'm trying to avoid that. It's a lot of bookkeeping that I don't need.

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.

Swift - UISearchBar in UIToolbar

How can I create A UISearchBar inside of a Toolbar in Swift? If I use the Interface Builder I get the following error:
error: Illegal Configuration: UISearchBar embedded in UIBarButtonItems (Only available in iPad documents)
Is there a Swift solution for this problem? The Toolbar will only be available on iPad.
UIToolBar is just a custom UIView, so you have three options (that I can see) for your implementation:
First, if you want to stick the search bar in the toolbar here's the code:
var searchBar = UISearchBar(frame: CGRectMake(0, 0, 100, 50))
var myView = UIView(frame: searchBar.frame)
myView.addSubview(searchBar)
var barButtonItem = [UIBarButtonItem(customView: myView)]
toolBar.setItems(barButtonItem, animated: false)
Second, if you want the search bar in the nav bar on your screen you can do the following:
let searchBar = UISearchBar(frame: CGRectMake(0, 0, 300, 50))
navigationController?.navigationBar.addSubview(searchBar)
Third, if you need the toolbar to be elsewhere, you can create your own custom UIView that looks like a toolbar (spans the width of the screen) and add the search bar to that. You can even make this new toolbar of yours a custom class so it's reusable throughout your program.
Cheers!

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

UIViewAnimation works in a single view controller but not when it is segued into from another view controller

I have an animation function that works perfectly fine when there is only a single View Controller. When it runs, the block on the left hand side moves to the right hand side. The code for the animation is below.
let timerSquare = UIView()
func timerRectangle(){
timerSquare.backgroundColor = UIColor.greenColor()
timerSquare.frame = CGRect(x: 0, y: 450, width: 50, height: 20)
self.view.addSubview(timerSquare)
UIView.animateWithDuration(2.0, animations:
{
self.timerSquare.backgroundColor = UIColor.redColor()
self.timerSquare.frame = CGRect(x: 270, y: 450, width: 50, height: 20)
}, completition: nil)
}
However the problem happens when I create a view controller with a simple button that segues into the view controller with the animation code. Once it segues into the code the animation doesnt run and it displays the block in its finish position as though the animation had already completed running.
The View controller has no other code other than the animation code and Ive placed the animation function in numerous parts of the view controller including the "ViewDidLoad" section and Im unsure why it doesnt run the animation from the initial position. It works perfectly fine on its own and the only thing different is creating a button seguing into the code so its puzzling me. Any help would be appreciated thanks
I managed to find a solution to the problem. I needed to add the subview in viewDidLoad and then start the animation in viewDidAppear so it would begin as soon as the View appeared. Hope this helped anyone else out there having the same problem

Resources