Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extension UILabel {
- convenience init(text: String? = nil,
- font: UIFont = .systemFont(ofSize: UIFont.labelFontSize),
- textColor: UIColor = .black,
- textAlignment: NSTextAlignment = .left,
- numberOfLines: Int = 1,
- backgroundColor: UIColor = .clear) {
- self.init()
- self.text = text
- self.font = font
- self.textColor = textColor
- self.textAlignment = textAlignment
- self.numberOfLines = numberOfLines
- self.backgroundColor = backgroundColor
- }
- }
- extension UIViewController {
- func configureLargeNavigationItemAppearance(for navigationItem: UINavigationItem) -> UINavigationItem {
- navigationItem.leftBarButtonItem = nil
- let label = UILabel(
- text: title,
- font: UIFont.systemFont(ofSize: 30, weight: .bold),
- textColor: .white,
- textAlignment: .left,
- numberOfLines: 1,
- backgroundColor: .clear
- )
- let view = UIView(frame: CGRect(x: 0, y: 0, width: navigationController?.navigationBar.frame.width ?? 0, height: navigationController?.navigationBar.frame.height ?? 0))
- view.addSubview(label)
- label.frame = CGRect(x: 5, y: 0, width: navigationController?.navigationBar.frame.width ?? 0, height: navigationController?.navigationBar.frame.height ?? 0)
- navigationItem.titleView = view
- return navigationItem
- }
- func configureModalNavigationItem(for navigationItem: UINavigationItem) -> UINavigationItem {
- let label = UILabel(
- text: title,
- font: UIFont.systemFont(ofSize: 20, weight: .semibold),
- textColor: .white,
- textAlignment: .center,
- numberOfLines: 1,
- backgroundColor: .clear
- )
- navigationItem.titleView = label
- navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "close"), target: self, action: #selector(dismissAnimated))
- return navigationItem
- }
- func configureDefaultNavigationItem(for navigationItem: UINavigationItem) -> UINavigationItem {
- let label = UILabel(
- text: title,
- font: UIFont.systemFont(ofSize: 20, weight: .semibold),
- textColor: .white,
- textAlignment: .center,
- numberOfLines: 1,
- backgroundColor: .clear
- )
- navigationItem.titleView = label
- navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "back-button"), target: self, action: #selector(popViewControllerAnimated))
- return navigationItem
- }
- @objc
- fileprivate func dismissAnimated() {
- dismiss(animated: true, completion: nil)
- }
- @objc
- fileprivate func popViewControllerAnimated() {
- navigationController?.popViewController(animated: true)
- }
- }
- extension UIBarButtonItem {
- /// Создает UIBarButtonItem с заданным цветом кнопки назад и действием по нажатию
- convenience init(image: UIImage, target: Any?, action: Selector, for controlEvents: UIControlEvents = .touchUpInside) {
- let button = UIButton(frame: CGRect(x: 0, y: 0, width: backButtonSize.width, height: backButtonSize.height))
- button.setImage(image, for: .normal)
- button.contentEdgeInsets = UIEdgeInsets(top: 0, left: leftInset, bottom: 0, right: 0)
- button.addTarget(target, action: action, for: controlEvents)
- button.isExclusiveTouch = true
- button.accessibilityIdentifier = UIBarButtonItem.backButtonAccessibilityIdentifier()
- self.init(customView: button)
- button.accessibilityIdentifier = UIBarButtonItem.backButtonAccessibilityIdentifier()
- }
- static func backButtonAccessibilityIdentifier() -> String {
- return "backButton"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement