Navigation bar Height is not changed in ViewDidLoad() programmatically? - swift

Hi I have tried to change the UINavigationBar height progrmatically it has not changed in viewDidLoad. but its working viewDidAppear() but once I have do my application in background after if open the app it has not changed it going default height.please any one help. thanks advance.
let bounds = self.navigationController!.navigationBar.bounds
self.navigationController!.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: 66)

Add UINavigationBar extension with your viewcontroller
extension UINavigationBar {
override open func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: UIScreen.main.bounds.size.width, height: 80.0)
}
}

Related

How to change the height of the naviagtionBar

In the latest version of xcode, I wrote the following code in the viewDidLoad to change the height of the NavigationBar.
self.navigationController?.navigationBar.frame = CGRect(x:0, y:0, width:UIScreen.main.bounds.size.width, height:40)
However, I couldn't change the height of the NavigationBar.
I would appreciate it if you could tell me how to solve it.
There are mainly 2 Approaches that i used
First Approach
Better to create Subclass of UINavigationController ... and add this method there .. Use this class as NavigationController
class MyNavigationController: UINavigationController {
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let height = CGFloat(72)
navigationBar.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: height)
}
}
Second workaround
You can create a Subclass of UINavigationBar like this
class MyNavigationBar: UINavigationBar {
override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width, height: 75)
}
}

Addding UIView between tabBar and Navigation view in UITabBarController

I am working on a project that uses a UITabBarController for displaying all the different UIViewControllers but now I need to add a mini player just in between the tabBar and the navigation view (ViewControllers will have to resize too).
Is there anyway I can achieve that by reusing the existing class?
EDIT
I have tried 2 methods:
1- Adding it into the view. Gets Added but above of the VCs
let aView = UIView()
view.addSubview(aView)
aView.backgroundColor = .white
aView.anchor(top: nil, leading: view.leadingAnchor, bottom: tabBar.topAnchor, trailing: view.trailingAnchor, size: .init(width: 0, height: 100))
2- Adding it into the tabBar. It might sound silly but I thought It would work.
let viewOverTabBar = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
viewOverTabBar.backgroundColor = UIColor.black
tabBar.addSubview(viewOverTabBar)
Add your view as subview to view of UITabBarViewController not tab bar itself. Just place it above tab bar.
Also change:
aView.anchor(top: nil, leading: view.leadingAnchor, bottom: tabBar.topAnchor, trailing: view.trailingAnchor, size: .init(width: 0, height: 100))
to setting directly frame property of your view.
Also you need to do in in viewWillAppear method.
You can try this way :
class MyTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.createSmallPlayer()
}
func createSmallPlayer() {
let viewOverTabBar = UIView(frame: CGRect(x: 0, y: self.tabBar.frame.origin.y-40, width: self.tabBar.frame.size.width, height: 30))
viewOverTabBar.backgroundColor = UIColor.brown
//viewOverTabBar.layer.cornerRadius = viewOverTabBar.frame.size.height/2
viewOverTabBar.layer.masksToBounds = false
viewOverTabBar.layer.shadowColor = UIColor.black.withAlphaComponent(0.5).cgColor
viewOverTabBar.layer.shadowRadius = 5.0
viewOverTabBar.layer.shadowOffset = CGSize(width: 0.0, height: -5.0)
viewOverTabBar.layer.shadowOpacity = 0.5
//tabBar.addSubview(viewOverTabBar)
view.addSubview(viewOverTabBar)
}
}
And make sure that your all other view controller(which will navigate within tabbar) adjust frame accordingly.
Either you have to manage bottom view of all view controller by 30 pixels up and keep 30 pixels space blank at bottom, so no any content hide behind your player view.
Or you have you add :
Container view UIView same as added player view.
In that you have to add view controller's view with navigation controller as subview(Refer this : Adding a view controller as a subview in another view controller).

Swift UItableView 8.4ver layout correct, 10.3ver is incorrect

I use the following code, Create UITableView programatically
override func viewDidLoad() {
self.view.addSubview(self.tableView)
let width = UIScreen.main.bounds.width+500
self.tableView.frame = CGRect(x: 0, y: 20, width: width, height:
self.view.frame.height)
}
8.4ver Layout:
10.3ver Layout:
Is it version bug? How can I fix this?
The width you are specifying is 500 pts. more than the width of the screen, so that needs to be fixed.
Assuming you want the tableview to fill the screen, you should get the height from the same place you got the width. This code works:
override func viewDidLoad() {
self.view.addSubview(self.tableView)
let width = UIScreen.main.bounds.width
self.tableView.frame = CGRect(x: 0, y: 20, width: width, height:
UIScreen.main.bounds.height - 20)
}

Cocoa NSScrollView not scrolling

I try to create a scrollview. Created a scrollview via Xib, programmatically added scrolledView (a NSView that contains all subview in scrollView).
The following code shows the subviews but the scrollView not scroll. Why?
class LevelScrollController: NSViewController {
#IBOutlet var scrollView: NSScrollView!
override func viewDidLoad() {
super.viewDidLoad()
let scrolledView = NSView(frame: NSRect(x: 0, y: 0, width: scrollView.frame.size.width, height: 300))
// Inserisco pulsanti di esempio
for i in 1 ... 10{
scrolledView.addSubview(NSButton(frame: NSRect(x: 0, y: i*30, width: 100, height: 30)))
}
scrollView.addSubview(scrolledView)
}
Putting the code in viewDidLayout instead of viewDidLoad not change the result: not scroll
Instead of add scrolledView to subview of scrollView, set it as documentView.
Replace scrollView.addSubview(scrolledView) with scrollView.documentView = scrolledView

adding a UIView as a SubView to a custom class, subclass of UIView, Swift

I am having the following problem.
I have a custom class, subclass of UIView
Within it, I try to add a new UIView, but it's not letting me.
class myCustomViewClass: UIView {
let smallerView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
self.view.addSubview(smallerView) // Error with this !
}
I have tried different things.
I know I can make it work if I subclass from UIViewController instead, but I don't really want to do that.
Appreciate any help. Thanks!
class myCustomViewClass: UIView {
let smallerView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
override func drawRect(rect: CGRect) {
self.addSubview(smallerView)
}
}