Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. extension UILabel {
  2.  
  3. convenience init(text: String? = nil,
  4. font: UIFont = .systemFont(ofSize: UIFont.labelFontSize),
  5. textColor: UIColor = .black,
  6. textAlignment: NSTextAlignment = .left,
  7. numberOfLines: Int = 1,
  8. backgroundColor: UIColor = .clear) {
  9. self.init()
  10. self.text = text
  11. self.font = font
  12. self.textColor = textColor
  13. self.textAlignment = textAlignment
  14. self.numberOfLines = numberOfLines
  15. self.backgroundColor = backgroundColor
  16. }
  17. }
  18.  
  19.  
  20. extension UIViewController {
  21. func configureLargeNavigationItemAppearance(for navigationItem: UINavigationItem) -> UINavigationItem {
  22. navigationItem.leftBarButtonItem = nil
  23.  
  24. let label = UILabel(
  25. text: title,
  26. font: UIFont.systemFont(ofSize: 30, weight: .bold),
  27. textColor: .white,
  28. textAlignment: .left,
  29. numberOfLines: 1,
  30. backgroundColor: .clear
  31. )
  32. let view = UIView(frame: CGRect(x: 0, y: 0, width: navigationController?.navigationBar.frame.width ?? 0, height: navigationController?.navigationBar.frame.height ?? 0))
  33. view.addSubview(label)
  34. label.frame = CGRect(x: 5, y: 0, width: navigationController?.navigationBar.frame.width ?? 0, height: navigationController?.navigationBar.frame.height ?? 0)
  35. navigationItem.titleView = view
  36. return navigationItem
  37. }
  38.  
  39. func configureModalNavigationItem(for navigationItem: UINavigationItem) -> UINavigationItem {
  40. let label = UILabel(
  41. text: title,
  42. font: UIFont.systemFont(ofSize: 20, weight: .semibold),
  43. textColor: .white,
  44. textAlignment: .center,
  45. numberOfLines: 1,
  46. backgroundColor: .clear
  47. )
  48. navigationItem.titleView = label
  49.  
  50. navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "close"), target: self, action: #selector(dismissAnimated))
  51.  
  52. return navigationItem
  53. }
  54.  
  55. func configureDefaultNavigationItem(for navigationItem: UINavigationItem) -> UINavigationItem {
  56. let label = UILabel(
  57. text: title,
  58. font: UIFont.systemFont(ofSize: 20, weight: .semibold),
  59. textColor: .white,
  60. textAlignment: .center,
  61. numberOfLines: 1,
  62. backgroundColor: .clear
  63. )
  64. navigationItem.titleView = label
  65.  
  66. navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "back-button"), target: self, action: #selector(popViewControllerAnimated))
  67.  
  68. return navigationItem
  69. }
  70.  
  71. @objc
  72. fileprivate func dismissAnimated() {
  73. dismiss(animated: true, completion: nil)
  74. }
  75.  
  76. @objc
  77. fileprivate func popViewControllerAnimated() {
  78. navigationController?.popViewController(animated: true)
  79. }
  80. }
  81.  
  82. extension UIBarButtonItem {
  83.  
  84. /// Создает UIBarButtonItem с заданным цветом кнопки назад и действием по нажатию
  85. convenience init(image: UIImage, target: Any?, action: Selector, for controlEvents: UIControlEvents = .touchUpInside) {
  86. let button = UIButton(frame: CGRect(x: 0, y: 0, width: backButtonSize.width, height: backButtonSize.height))
  87. button.setImage(image, for: .normal)
  88. button.contentEdgeInsets = UIEdgeInsets(top: 0, left: leftInset, bottom: 0, right: 0)
  89. button.addTarget(target, action: action, for: controlEvents)
  90. button.isExclusiveTouch = true
  91. button.accessibilityIdentifier = UIBarButtonItem.backButtonAccessibilityIdentifier()
  92.  
  93. self.init(customView: button)
  94.  
  95. button.accessibilityIdentifier = UIBarButtonItem.backButtonAccessibilityIdentifier()
  96. }
  97.  
  98. static func backButtonAccessibilityIdentifier() -> String {
  99. return "backButton"
  100. }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement