Skip to content

Instantly share code, notes, and snippets.

@pai911
Last active August 27, 2016 19:16
Show Gist options
  • Save pai911/8fa123d4068b61ad0ff7 to your computer and use it in GitHub Desktop.
Save pai911/8fa123d4068b61ad0ff7 to your computer and use it in GitHub Desktop.
Custom Navigation Bar with increased height (tested on iOS 8.0)
//
// UINavigationBarTaller.swift
//
import UIKit
class UINavigationBarTaller: UINavigationBar {
///The height you want your navigation bar to be of
static let navigationBarHeight: CGFloat = 64
///The difference between new height and default height
static let heightIncrease:CGFloat = navigationBarHeight - 44
override init(frame: CGRect) {
super.init(frame: frame)
initialize()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialize()
}
private func initialize() {
let shift = UINavigationBarTaller.heightIncrease/2
///Transform all view to shift upward for [shift] point
self.transform =
CGAffineTransformMakeTranslation(0, -shift)
}
override func layoutSubviews() {
super.layoutSubviews()
let shift = UINavigationBarTaller.heightIncrease/2
///Move the background down for [shift] point
let classNamesToReposition: [String] = ["_UINavigationBarBackground"]
for view: UIView in self.subviews {
if classNamesToReposition.contains(NSStringFromClass(view.dynamicType)) {
let bounds: CGRect = self.bounds
var frame: CGRect = view.frame
frame.origin.y = bounds.origin.y + shift - 20.0
frame.size.height = bounds.size.height + 20.0
view.frame = frame
}
}
}
override func sizeThatFits(size: CGSize) -> CGSize {
let amendedSize:CGSize = super.sizeThatFits(size)
let newSize:CGSize = CGSizeMake(amendedSize.width, UINavigationBarTaller.navigationBarHeight);
return newSize;
}
}
@sgandro
Copy link

sgandro commented May 25, 2016

Hi,
I've need to move logo image in titleView on left, You can help me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment