Unë kam një skenar ku më nevojitet për të krijuar një UIView plotësisht programatikisht. Kjo pamje shfaqet si një mbivendosje mbi një furnizim video dhe përmban 2 etiketa UIL si nënshikime.
Këto etiketa mund të kenë gjerësi dhe lartësi të ndryshme, kështu që unë nuk dua të krijoj një kornizë fikse për UIView ose etiketat.
Unë e kam bërë këtë me kufizime programatike pa problem, por problemi është se pamjet janë të gabuara kur lëviz telefonin dhe korniza që po përdor, e cila rekomandon që furnizimi i videos të mos përdoret automatikisht për shkak të këtij problemi.
Kodi im aktual:
Unë krijoj pamjen
func ar(_ arViewController: ARViewController, viewForAnnotation: ARAnnotation) -> ARAnnotationView { let annotationView = AnnotationView() //*this is the view, inherits from UIView annotationView.annotation = viewForAnnotation annotationView.delegate = self return annotationView }
Dhe kodi aktual i pamjes
class AnnotationView: ARAnnotationView { var titleLabel: UILabel? var distanceLabel: UILabel! var delegate: AnnotationViewDelegate? override var annotation: ARAnnotation? { didSet { loadUI() } } override func didMoveToSuperview() { super.didMoveToSuperview() setupUI() } private func setupUI() { layer.cornerRadius = 5 layer.masksToBounds = true backgroundColor = .transparentWhite } private func loadUI() { titleLabel?.removeFromSuperview() distanceLabel?.removeFromSuperview() let label = UILabel() label.font = UIFont(name: "AvenirNext-Medium", size: 16.0) ?? UIFont.systemFont(ofSize: 14) label.numberOfLines = 2 label.textColor = UIColor.white self.titleLabel = label distanceLabel = UILabel() distanceLabel.textColor = .cbPPGreen distanceLabel.font = UIFont(name: "AvenirNext-Medium", size: 14.0) ?? UIFont.systemFont(ofSize: 12) distanceLabel.textAlignment = .center self.translatesAutoresizingMaskIntoConstraints = false label.translatesAutoresizingMaskIntoConstraints = false distanceLabel.translatesAutoresizingMaskIntoConstraints = false self.addSubview(label) self.addSubview(distanceLabel) if self.constraints.isEmpty { NSLayoutConstraint.activate([ NSLayoutConstraint(item: label, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: 5), NSLayoutConstraint(item: label, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1, constant: -5), NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0), NSLayoutConstraint(item: label, attribute: .bottom, relatedBy: .equal, toItem: distanceLabel, attribute: .top, multiplier: 1, constant: 0), NSLayoutConstraint(item: distanceLabel, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: 0), NSLayoutConstraint(item: distanceLabel, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1, constant: 0), NSLayoutConstraint(item: distanceLabel, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0), NSLayoutConstraint(item: distanceLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 15), ]) } if let annotation = annotation as? BikeStationAR { titleLabel?.text = annotation.address distanceLabel?.text = String(format: "%.2f km", annotation.distanceFromUser / 1000) } } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { delegate?.didTouch(annotationView: self) } }
Pra, problemi im është tani: si mund të arrij të njëjtën sjellje, pa përdorur autolayout?
Edit: këtu është një video e sjelljes https://streamable.com/s/4zusq/dvbgnb
Redaktimi 2: Zgjidhja bazohet në përgjigjen e Mahgol Fa dhe duke përdorur një pamje rafte ose pozicionim vertikal në mënyrë që të mos vendoset korniza për etiketat individuale. Pra, zbatimi përfundimtar është
private func loadUI() {
titleLabel?.removeFromSuperview()
distanceLabel?.removeFromSuperview()
let label = UILabel()
label.font = titleViewFont
label.numberOfLines = 0
label.textColor = UIColor.white
label.textAlignment = .center
self.titleLabel = label
distanceLabel = UILabel()
distanceLabel.textColor = .cbPPGreen
distanceLabel.font = subtitleViewFont
distanceLabel.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
distanceLabel.translatesAutoresizingMaskIntoConstraints = false
if let annotation = annotation as? BikeStationAR {
let title = annotation.address
let subtitle = String(format: "%.2f km", annotation.distanceFromUser / 1000)
let fontAttributeTitle = [NSAttributedString.Key.font: titleViewFont]
let titleSize = title.size(withAttributes: fontAttributeTitle)
let fontAttributeSubtitle = [NSAttributedString.Key.font: subtitleViewFont]
let subtitleSize = annotation.address.size(withAttributes: fontAttributeSubtitle)
titleLabel?.text = title
distanceLabel?.text = subtitle
let stackView = UIStackView(frame: CGRect(x: 0, y: 0, width: titleSize.width + 5, height: titleSize.height + subtitleSize.height + 5))
stackView.axis = .vertical
stackView.distribution = .fillProportionally
stackView.alignment = .fill
stackView.addArrangedSubview(titleLabel ?? UIView())
stackView.addArrangedSubview(distanceLabel)
frame = stackView.bounds
addSubview(stackView)
}
}