[iOS] Fixed aligned buttons in menu

https://jira.mail.ru/browse/MAPSME-13988
This commit is contained in:
Alexander Boriskov 2020-06-08 13:26:57 +03:00 committed by Vladimir Byko-Ianko
parent 2a61de53ce
commit d865e29232
5 changed files with 122 additions and 92 deletions

View file

@ -1,44 +1,86 @@
@IBDesignable
class VerticallyAlignedButton: MWMButton {
class VerticallyAlignedButton: UIControl {
@IBInspectable
var spacing: CGFloat = 4
@IBInspectable
var numberOfLines: Int {
get {
return titleLabel?.numberOfLines ?? 0
}
set {
titleLabel?.numberOfLines = newValue
var image: UIImage? {
didSet {
imageView.image = image
}
}
@IBInspectable
var title: String? {
didSet {
if localizedText == nil {
titleLabel.text = title
}
}
}
@IBInspectable
var localizedText: String? {
didSet {
if let localizedText = localizedText {
titleLabel.text = L(localizedText)
}
}
}
@IBInspectable
var spacing: CGFloat = 4 {
didSet {
spacingConstraint.constant = spacing
}
}
@IBInspectable
var numberOfLines: Int {
get {
return titleLabel.numberOfLines
}
set {
titleLabel.numberOfLines = newValue
}
}
private lazy var spacingConstraint: NSLayoutConstraint = {
let spacingConstraint = titleLabel.topAnchor.constraint(equalTo: imageView.bottomAnchor, constant: spacing)
return spacingConstraint
}()
lazy var titleLabel: UILabel = {
let titleLabel = UILabel()
titleLabel.textAlignment = .center
titleLabel.translatesAutoresizingMaskIntoConstraints = false
return titleLabel
}()
lazy var imageView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .center
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
override init(frame: CGRect) {
super.init(frame: frame)
titleLabel?.textAlignment = .center
setupView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
titleLabel?.textAlignment = .center
setupView()
}
override func layoutSubviews() {
super.layoutSubviews()
updateView()
}
private func setupView() {
addSubview(titleLabel)
addSubview(imageView)
private func updateView() {
let imageSize = self.imageView?.frame.size ?? .zero
let titleSize = self.titleLabel?.frame.size ?? .zero
let size = self.size
let height = imageSize.height + titleSize.height + spacing
self.imageEdgeInsets = UIEdgeInsets(top: -(size.height - imageSize.height),
left: 0,
bottom: 0,
right: -titleSize.width)
self.titleEdgeInsets = UIEdgeInsets(top: height,
left: -imageSize.width,
bottom: 0,
right: 0)
NSLayoutConstraint.activate([
imageView.topAnchor.constraint(equalTo: topAnchor),
imageView.centerXAnchor.constraint(equalTo: centerXAnchor),
titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor),
titleLabel.trailingAnchor.constraint(equalTo: trailingAnchor),
spacingConstraint
])
}
}

View file

@ -9,18 +9,14 @@ extension BottomMenuLayerButton {
class BottomMenuLayerButtonRenderer {
class func render(_ control: BottomMenuLayerButton, style: Style) {
if let titleLabel = control.titleLabel {
if let font = style.font {
titleLabel.font = font
}
if let font = style.font {
control.titleLabel.font = font
}
if let fontColor = style.fontColor {
control.setTitleColor(fontColor, for: .normal)
control.titleLabel.textColor = fontColor
}
if let imageView = control.imageView {
UIImageViewRenderer.render(imageView, style: style)
}
UIImageViewRenderer.render(control.imageView, style: style)
}
}

View file

@ -20,7 +20,7 @@ final class BottomMenuLayerButton: VerticallyAlignedButton {
if isBadgeHidden {
badgeView?.removeFromSuperview()
badgeView = nil
} else if let imageView = imageView{
} else {
if badgeView == nil {
badgeView = UIView()
badgeView?.styleName = "Badge"

View file

@ -70,7 +70,7 @@ class BottomMenuLayersCell: UITableViewCell {
kStatFrom : source,
kStatTurnOn : enable])
}
@IBAction func onTrafficButton(_ sender: UIButton) {
@IBAction func onTrafficButton(_ sender: Any) {
let enable = !MapOverlayManager.trafficEnabled()
MapOverlayManager.setTrafficEnabled(enable)
Statistics.logEvent(kStatLayersClick, withParameters: [kStatName : kStatTraffic,
@ -78,7 +78,7 @@ class BottomMenuLayersCell: UITableViewCell {
kStatTurnOn : enable])
}
@IBAction func onSubwayButton(_ sender: UIButton) {
@IBAction func onSubwayButton(_ sender: Any) {
let enable = !MapOverlayManager.transitEnabled()
MapOverlayManager.setTransitEnabled(enable)
Statistics.logEvent(kStatLayersClick, withParameters: [kStatName : kStatSubway,
@ -86,7 +86,7 @@ class BottomMenuLayersCell: UITableViewCell {
kStatTurnOn : enable])
}
@IBAction func onIsoLinesButton(_ sender: UIButton) {
@IBAction func onIsoLinesButton(_ sender: Any) {
let enable = !MapOverlayManager.isoLinesEnabled()
MapOverlayManager.setIsoLinesEnabled(enable)
Statistics.logEvent(kStatLayersClick, withParameters: [kStatName : kStatIsolines,

View file

@ -74,82 +74,74 @@
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" translatesAutoresizingMaskIntoConstraints="NO" id="sRd-zd-xSl">
<rect key="frame" x="16" y="58" width="308" height="64"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="HNT-jM-BGw" customClass="BottomMenuLayerButton" customModule="maps_me" customModuleProvider="target">
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="PoD-TJ-E1s" customClass="BottomMenuLayerButton" customModule="maps_me" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="77" height="64"/>
<fontDescription key="fontDescription" type="system" pointSize="10"/>
<state key="normal" title="Guides" image="btn_menu_guides">
<color key="titleColor" white="0.66666666669999997" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</state>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="image" keyPath="image" value="btn_menu_guides"/>
<userDefinedRuntimeAttribute type="number" keyPath="spacing">
<real key="value" value="10"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="numberOfLines">
<integer key="value" value="2"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="string" keyPath="localizedText" value="button_layer_guides"/>
<userDefinedRuntimeAttribute type="number" keyPath="spacing">
<real key="value" value="0.0"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="numberOfLines">
<integer key="value" value="2"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
<connections>
<action selector="onGuidesButtonPressed:" destination="KGk-i7-Jjw" eventType="touchUpInside" id="bcD-pp-RtJ"/>
<action selector="onGuidesButtonPressed:" destination="KGk-i7-Jjw" eventType="touchUpInside" id="DEl-Vg-Pdk"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="3Ms-FI-jtO" customClass="BottomMenuLayerButton" customModule="maps_me" customModuleProvider="target">
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="edA-Mo-3Vx" customClass="BottomMenuLayerButton" customModule="maps_me" customModuleProvider="target">
<rect key="frame" x="77" y="0.0" width="77" height="64"/>
<fontDescription key="fontDescription" type="system" pointSize="10"/>
<state key="normal" title="Contour" image="btn_menu_isomaps">
<color key="titleColor" white="0.66666666669999997" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</state>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="image" keyPath="image" value="btn_menu_isomaps"/>
<userDefinedRuntimeAttribute type="number" keyPath="spacing">
<real key="value" value="10"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="numberOfLines">
<integer key="value" value="2"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="string" keyPath="localizedText" value="button_layer_isolines"/>
<userDefinedRuntimeAttribute type="number" keyPath="spacing">
<real key="value" value="0.0"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="numberOfLines">
<integer key="value" value="2"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
<connections>
<action selector="onIsoLinesButton:" destination="KGk-i7-Jjw" eventType="touchUpInside" id="cRN-3C-Hw7"/>
<action selector="onIsoLinesButton:" destination="KGk-i7-Jjw" eventType="touchUpInside" id="3LS-C2-2Mc"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="iib-us-Ysk" customClass="BottomMenuLayerButton" customModule="maps_me" customModuleProvider="target">
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="4US-fZ-cyg" customClass="BottomMenuLayerButton" customModule="maps_me" customModuleProvider="target">
<rect key="frame" x="154" y="0.0" width="77" height="64"/>
<fontDescription key="fontDescription" type="system" pointSize="10"/>
<state key="normal" title="Subway" image="btn_menu_subway">
<color key="titleColor" white="0.66666666669999997" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</state>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="image" keyPath="image" value="btn_menu_subway"/>
<userDefinedRuntimeAttribute type="number" keyPath="spacing">
<real key="value" value="10"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="numberOfLines">
<integer key="value" value="2"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="string" keyPath="localizedText" value="button_layer_subway"/>
<userDefinedRuntimeAttribute type="number" keyPath="spacing">
<real key="value" value="0.0"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="numberOfLines">
<integer key="value" value="2"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
<connections>
<action selector="onSubwayButton:" destination="KGk-i7-Jjw" eventType="touchUpInside" id="V1a-gI-mJn"/>
<action selector="onSubwayButton:" destination="KGk-i7-Jjw" eventType="touchUpInside" id="xxM-kP-gT1"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="7Si-54-ubF" customClass="BottomMenuLayerButton" customModule="maps_me" customModuleProvider="target">
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="X9u-oF-oz8" customClass="BottomMenuLayerButton" customModule="maps_me" customModuleProvider="target">
<rect key="frame" x="231" y="0.0" width="77" height="64"/>
<fontDescription key="fontDescription" type="system" pointSize="10"/>
<state key="normal" title="Traffic" image="btn_menu_traffic">
<color key="titleColor" white="0.66666666669999997" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</state>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="string" keyPath="localizedText" value="button_layer_traffic"/>
<userDefinedRuntimeAttribute type="image" keyPath="image" value="btn_menu_traffic"/>
<userDefinedRuntimeAttribute type="number" keyPath="spacing">
<real key="value" value="0.0"/>
<real key="value" value="10"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="numberOfLines">
<integer key="value" value="2"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="string" keyPath="localizedText" value="button_layer_traffic"/>
</userDefinedRuntimeAttributes>
<connections>
<action selector="onTrafficButton:" destination="KGk-i7-Jjw" eventType="touchUpInside" id="x9b-yU-SjG"/>
<action selector="onTrafficButton:" destination="KGk-i7-Jjw" eventType="touchUpInside" id="sfg-OY-ECY"/>
</connections>
</button>
</view>
</subviews>
<constraints>
<constraint firstAttribute="height" constant="70" id="d0H-kE-IWx"/>
@ -204,10 +196,10 @@
</constraints>
<viewLayoutGuide key="safeArea" id="njF-e1-oar"/>
<connections>
<outlet property="guidesButton" destination="HNT-jM-BGw" id="gVI-fz-NNN"/>
<outlet property="isoLinesButton" destination="3Ms-FI-jtO" id="mV3-6F-KEB"/>
<outlet property="subwayButton" destination="iib-us-Ysk" id="Kma-Pu-v5j"/>
<outlet property="trafficButton" destination="7Si-54-ubF" id="Xt8-Po-RsO"/>
<outlet property="guidesButton" destination="PoD-TJ-E1s" id="azk-HY-F5J"/>
<outlet property="isoLinesButton" destination="edA-Mo-3Vx" id="qoC-8w-EqY"/>
<outlet property="subwayButton" destination="4US-fZ-cyg" id="eQB-HR-Wgl"/>
<outlet property="trafficButton" destination="X9u-oF-oz8" id="rd3-S2-col"/>
</connections>
<point key="canvasLocation" x="137.6953125" y="201.953125"/>
</tableViewCell>