代码之家  ›  专栏  ›  技术社区  ›  Zack Shapiro

如何将此导航项标题向左移动?

  •  0
  • Zack Shapiro  · 技术社区  · 7 年前

    我有一个当前居中的标题。我的目标是把它移到左边,这样它就位于后箭头右边的右边。

    titleView UINavigationBar

    代码如下。任何帮助都将不胜感激。

    enter image description here

    class GroupMessageTitleView: UIControl {
    
        struct Constants {
            static let imageSize = CGSize(width: 36.0, height: 36.0)
            static let spacingBetweenImageAndText: CGFloat = 8.0
            static let viewHeight: CGFloat = 40.0
        }
    
        // MARK: - Views
    
        private let imageView: UIImageView = {
            let frame = CGRect(origin: .zero, size: Constants.imageSize)
            let imageView = UIImageView(frame: frame)
            imageView.layer.cornerRadius = Constants.imageSize.height / 2
            imageView.clipsToBounds = true
            imageView.backgroundColor = UIColor.blue.withAlphaComponent(0.4)
            return imageView
        }()
    
        private let titleLabel: UILabel = {
            let label = UILabel()
            label.font = Styleguide.Fonts.pingFangMedium.font(ofSize: 16)
            label.backgroundColor = UIColor.red.withAlphaComponent(0.4)
            return label
        }()
    
        private let subtitleLabel: UILabel = {
            let label = UILabel()
            label.textColor = UIColor.from(rgb: 0xCDCDCD)
            label.font = Styleguide.Fonts.pingFangMedium.font(ofSize: 14)
            label.backgroundColor = UIColor.green.withAlphaComponent(0.4)
            return label
        }()
    
        private lazy var stackView: UIStackView = {
            let stackView = UIStackView(arrangedSubviews: [titleLabel, subtitleLabel])
            stackView.spacing = -4.0
            stackView.axis = .vertical
            stackView.alignment = .leading
            return stackView
        }()
    
        private lazy var horizontalStackView: UIStackView = {
            let stackView = UIStackView(arrangedSubviews: [imageView, self.stackView])
            stackView.spacing = Constants.spacingBetweenImageAndText
            stackView.axis = .horizontal
            stackView.alignment = .center
            stackView.translatesAutoresizingMaskIntoConstraints = false
            stackView.isUserInteractionEnabled = false
            return stackView
        }()
    
        // MARK: - Properties
    
        weak var delegate: GroupMessageTitleViewDelegate?
    
        override var isHighlighted: Bool {
            didSet {
                if isHighlighted != oldValue { highlightStateDidChange() }
            }
        }
    
        // MARK: - Initializers
    
        override init(frame: CGRect) {
    
            let initialFrame: CGRect
    
            if #available(iOS 11.1, *) {
                initialFrame = .zero
            } else {
                // Arbitrary Non Zero Frame. To please the Apple Gods.
                // When the frame is set to .zero on iOS 10,
                // No matter what we do in specfying a size in
                // sizeThatFits or intrinsicContentSize, the frame is not updated
                // Once it's non zero, the frame is then set correctly
                // using sizeThatFits for the size
                initialFrame = CGRect(x: 0, y: 0, width: 0, height: Constants.viewHeight)
            }
    
            super.init(frame: initialFrame)
    
            addSubview(horizontalStackView)
    
            addTarget(self, action: #selector(handleTap), for: .touchUpInside)
    
            setupConstraints()
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        // MARK: - View Configuration
    
        func configure(with presentable: GroupMessageTitlePresentable) {
            titleLabel.text = presentable.name
    
            let members = "member".pluralize(withCount: presentable.memberCount).localize()
            subtitleLabel.text = "\(presentable.memberCount) \(members)"
    
            imageView.setImage(with: presentable.imageUrl, fallbackText: presentable.name, fallbackImageSize: Constants.imageSize.height)
        }
    
        func highlightStateDidChange() {
            UIView.animate(withDuration: 0.15, delay: 0, options: .beginFromCurrentState, animations: {
                self.alpha = self.isHighlighted ? 0.5 : 1.0
            }, completion: nil)
        }
    
        // MARK: - Layout
    
        func setupConstraints() {
            constrain(horizontalStackView) { view in
                // to ensure it is correctly centered, we offset by half the width of the imageView
                let insets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: Constants.imageSize.width / 2)
                view.edges == inset(view.superview!.edges, insets)
            }
    
            constrain(imageView) { view in
                view.width == Constants.imageSize.width
                view.height == Constants.imageSize.height ~ .required
            }
        }
    
        // MARK: - Sizing Methods
    
        override func sizeThatFits(_ size: CGSize) -> CGSize {
            if #available(iOS 11.1, *) {
                return super.sizeThatFits(size)
            } else {
                // Image Width + Spacing + Title Label Width + Trailing inset
                let width = Constants.imageSize.width + Constants.spacingBetweenImageAndText + self.titleLabel.sizeThatFits(size).width + (Constants.imageSize.width / 2)
                return CGSize(width: width, height: Constants.viewHeight)
            }
        }
    
        // MARK: - Touch Handling
    
        @objc func handleTap() {
            delegate?.didTap(titleView: self)
        }
    
    }
    
    0 回复  |  直到 7 年前