У меня есть этот заголовок, который в настоящее время центрирован.И моя цель - сместить его влево, чтобы он находился справа от правого поля стрелки назад.
В конечном счете, представление ниже устанавливается в titleView
кода UINavigationBar
ниже.Любая помощь будет принята с благодарностью.
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)
}
}