[ios] improved display of hotel types in search by re-using custom layout

This commit is contained in:
o.bolovintseva 2019-01-16 16:57:22 +03:00 committed by Aleksey Belousov
parent 36f4c2c896
commit b8ec365a66
5 changed files with 47 additions and 46 deletions

View file

@ -17,10 +17,10 @@ final class TagsCollectionViewLayout: UICollectionViewLayout {
return CGSize(width: contentWidth, height: contentHeight)
}
var itemHeight: CGFloat = 50
var itemWidth: CGFloat = 50
var lineSpacing: CGFloat = 10
var elementsSpacing: CGFloat = 10
@IBInspectable var itemHeight: CGFloat = 50
@IBInspectable var itemWidth: CGFloat = 50
@IBInspectable var lineSpacing: CGFloat = 10
@IBInspectable var elementsSpacing: CGFloat = 10
override func prepare() {
super.prepare()
@ -35,17 +35,21 @@ final class TagsCollectionViewLayout: UICollectionViewLayout {
var lastItemHeight: CGFloat = 0
for section in 0 ..< collectionView.numberOfSections {
xOffset = 0
yOffset += (section == 0) ? 0 : lastItemHeight + 2 * lineSpacing
let indexPath = IndexPath(item: 0, section: section)
let headerSize = headersCache[indexPath]?.size ?? CGSize(width: contentWidth, height: itemHeight)
let frame = CGRect(x: 0, y: yOffset, width: headerSize.width, height: headerSize.height)
let attr = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, with: indexPath)
attr.frame = frame
headersCache[indexPath] = attr
yOffset += headerSize.height + lineSpacing
xOffset = 0
if collectionView.supplementaryView(forElementKind: UICollectionElementKindSectionHeader,
at: indexPath) != nil {
let headerSize = headersCache[indexPath]?.size ?? CGSize(width: contentWidth, height: itemHeight)
let frame = CGRect(x: 0, y: yOffset, width: headerSize.width, height: headerSize.height)
let attr = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, with: indexPath)
attr.frame = frame
headersCache[indexPath] = attr
yOffset += headerSize.height + lineSpacing
}
for item in 0 ..< collectionView.numberOfItems(inSection: section) {
let indexPath = IndexPath(item: item, section: section)

View file

@ -25,6 +25,7 @@ final class FilterCollectionHolderCell: MWMTableViewCell {
self.tableView = tableView
layout()
collectionView.allowsMultipleSelection = true
collectionView.contentInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
collectionView.reloadData()
}

View file

@ -1,23 +1,3 @@
final class TypeCellFlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let attributes = super.layoutAttributesForElements(in: rect)
var leftMargin = sectionInset.left
var maxY: CGFloat = 0
attributes?.forEach { layoutAttribute in
if layoutAttribute.frame.origin.y >= maxY {
leftMargin = sectionInset.left
}
layoutAttribute.frame.origin.x = leftMargin
leftMargin += layoutAttribute.frame.width + minimumInteritemSpacing
maxY = max(layoutAttribute.frame.maxY, maxY)
}
return attributes
}
}
@objc(MWMFilterTypeCell)
final class FilterTypeCell: UICollectionViewCell {

View file

@ -477,6 +477,13 @@ void configButton(UIButton * button, NSString * primaryText, NSString * secondar
auto const type = kTypes[indexPath.row];
auto str = [NSString stringWithFormat:kHotelTypePattern, @(ftypes::IsHotelChecker::GetHotelTypeTag(type))];
cell.tagName.text = L(str);
//we need to do this because of bug - ios 12 doesnt apply layout to cells until scrolling
if (@available(iOS 12.0, *))
{
[cell layoutIfNeeded];
}
auto const selected = m_selectedTypes.find(type) != m_selectedTypes.end();
cell.selected = selected;
if (selected)

View file

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
@ -13,7 +13,7 @@
<!--Search Hotels Filter View Controller-->
<scene sceneID="3Lw-zP-9hH">
<objects>
<viewController storyboardIdentifier="MWMSearchHotelsFilterViewController" useStoryboardIdentifierAsRestorationIdentifier="YES" id="LNn-Gp-Jtg" customClass="MWMSearchHotelsFilterViewController" propertyAccessControl="all" sceneMemberID="viewController">
<viewController storyboardIdentifier="MWMSearchHotelsFilterViewController" useStoryboardIdentifierAsRestorationIdentifier="YES" id="LNn-Gp-Jtg" customClass="MWMSearchHotelsFilterViewController" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="a4S-IT-Fgz">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
@ -30,13 +30,13 @@
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="7zd-JW-kVE">
<rect key="frame" x="16" y="8" width="42" height="20.5"/>
<rect key="frame" x="16" y="8" width="42" height="14.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<button opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="5zu-QE-OXk">
<rect key="frame" x="16" y="32.5" width="167.5" height="36"/>
<rect key="frame" x="16" y="26.5" width="167.5" height="36"/>
<constraints>
<constraint firstAttribute="height" constant="36" id="H9S-4G-HPV"/>
</constraints>
@ -47,20 +47,20 @@
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="WZ7-6d-CsM">
<rect key="frame" x="191.5" y="8" width="42" height="20.5"/>
<rect key="frame" x="191.5" y="2" width="42" height="20.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<button opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="nOA-aX-aet">
<rect key="frame" x="191.5" y="32.5" width="167.5" height="36"/>
<rect key="frame" x="191.5" y="26.5" width="167.5" height="36"/>
<inset key="contentEdgeInsets" minX="8" minY="0.0" maxX="0.0" maxY="0.0"/>
<state key="normal" title="Button"/>
<connections>
<action selector="tapWithSender:" destination="kQ5-BZ-yUa" eventType="touchUpInside" id="O1l-vW-dJm"/>
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="pAF-QN-4y0">
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" ambiguous="YES" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="pAF-QN-4y0">
<rect key="frame" x="16" y="76.5" width="42" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
@ -189,7 +189,7 @@
<autoresizingMask key="autoresizingMask"/>
<subviews>
<button opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="wordWrap" translatesAutoresizingMaskIntoConstraints="NO" id="cm9-a2-KtE">
<rect key="frame" x="16" y="0.0" width="109" height="40"/>
<rect key="frame" x="16" y="0.0" width="109" height="39.5"/>
<constraints>
<constraint firstAttribute="height" priority="999" constant="40" id="ntq-iz-6Ov"/>
</constraints>
@ -200,7 +200,7 @@
</connections>
</button>
<button opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="wordWrap" translatesAutoresizingMaskIntoConstraints="NO" id="eEn-77-iwI">
<rect key="frame" x="133" y="0.0" width="109.5" height="40"/>
<rect key="frame" x="133" y="0.0" width="109" height="39.5"/>
<color key="tintColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<state key="normal" title="$$"/>
<connections>
@ -208,7 +208,7 @@
</connections>
</button>
<button opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="wordWrap" translatesAutoresizingMaskIntoConstraints="NO" id="96b-HD-t1A">
<rect key="frame" x="250.5" y="0.0" width="108.5" height="40"/>
<rect key="frame" x="250" y="0.0" width="109" height="39.5"/>
<color key="tintColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<state key="normal" title="$$$"/>
<connections>
@ -257,14 +257,23 @@
<constraints>
<constraint firstAttribute="height" priority="999" constant="43.5" id="wiY-V3-s47"/>
</constraints>
<collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="12" minimumInteritemSpacing="8" id="hQi-pH-6ln" customClass="TypeCellFlowLayout" customModule="maps_me" customModuleProvider="target">
<collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="12" minimumInteritemSpacing="8" id="hQi-pH-6ln" customClass="TagsCollectionViewLayout" customModule="maps_me" customModuleProvider="target">
<size key="itemSize" width="50" height="36"/>
<size key="headerReferenceSize" width="0.0" height="0.0"/>
<size key="footerReferenceSize" width="0.0" height="0.0"/>
<inset key="sectionInset" minX="16" minY="0.0" maxX="16" maxY="0.0"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="size" keyPath="estimatedItemSize">
<size key="value" width="100" height="36"/>
<userDefinedRuntimeAttribute type="number" keyPath="itemHeight">
<real key="value" value="50"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="itemWidth">
<real key="value" value="36"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="lineSpacing">
<real key="value" value="12"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="elementsSpacing">
<real key="value" value="8"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</collectionViewFlowLayout>