Last active
February 25, 2022 09:36
-
-
Save brascene/7265a1994b064a781dacf7d76ee5f0df to your computer and use it in GitHub Desktop.
Navigation bar large title custom y offset - iOS 11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class LargeTitleCustomOffset: UINavigationBar { | |
let labelcolor = UIColor(red: 36.0/255.0, green: 38.0/255.0, blue: 47.0/255.0, alpha: 1.0) | |
override func draw(_ rect: CGRect) { | |
super.draw(rect) | |
self.backgroundColor = UIColor.white | |
let largeView = "_UINavigationBarLargeTitleView" | |
for view in self.subviews { | |
if largeView.contains(String(describing: type(of: view))) { | |
for v in view.subviews { | |
if String(describing: type(of: v)) == "UILabel" { | |
var titleLabel = v as! UILabel | |
var labelRect = titleLabel.frame | |
let labelInsets = UIEdgeInsets(top: 10, left: 13, bottom: 0, right: 0) | |
let attrText = NSMutableAttributedString(string: "Custom title", attributes: [NSAttributedStringKey.font: UIFont(name: "SFProDisplay-Heavy", size: 30)!, NSAttributedStringKey.foregroundColor: self.labelcolor]) | |
labelRect.origin.y += 20 | |
let newLabel = UILabel(frame: labelRect) | |
newLabel.attributedText = attrText | |
titleLabel.text = "" | |
// disable offset if normal title state is on | |
if labelRect.origin.y > 0 { | |
titleLabel = newLabel | |
titleLabel.drawText(in: UIEdgeInsetsInsetRect(labelRect, labelInsets)) | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment