Skip to content

Commit

Permalink
Fix AutoLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
YuigaWada committed Aug 29, 2019
1 parent 5a4c0a0 commit 4bc0b3b
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 44 deletions.
2 changes: 1 addition & 1 deletion PolioPager.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "PolioPager"
s.version = "1.11"
s.version = "1.1.2"
s.summary = "PolioPager enables us to use PagerTabStrip like SNKRS."
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.homepage = "https://github.com/yuigawada/PolioPager"
Expand Down
74 changes: 67 additions & 7 deletions PolioPager/PolioPagerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ open class PolioPagerViewController: UIViewController, TabCellDelegate, PolioPag


//MARK: Var
private var defaultCellHeight: CGFloat = 50
private lazy var bundle = Bundle(for: PolioPagerViewController.self)
private var itemsFrame: [CGRect] = []
private var itemsWidths: [CGFloat] = []
Expand Down Expand Up @@ -82,8 +83,16 @@ open class PolioPagerViewController: UIViewController, TabCellDelegate, PolioPag
self.initialIndex += searchTab ? 1 : 0 //TODO: ここいる?
self.pageViewController.parentVC = self

defaultCellHeight = self.collectionView.frame.height

setupCell()
setupPageView()

}

override open func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

setTabItem(tabItems())
}

Expand Down Expand Up @@ -137,15 +146,59 @@ open class PolioPagerViewController: UIViewController, TabCellDelegate, PolioPag

private func setupPageView()
{
pageViewController.view.frame = self.pageView.frame
pageViewController.view.translatesAutoresizingMaskIntoConstraints = false

self.pageView.addSubview(pageViewController.view)


//AutoLayout
self.pageView.addConstraints([
NSLayoutConstraint(item: pageViewController.view,
attribute: NSLayoutConstraint.Attribute.centerX,
relatedBy: NSLayoutConstraint.Relation.equal,
toItem: self.pageView ,
attribute: NSLayoutConstraint.Attribute.centerX,
multiplier: 1.0,
constant: 0),

NSLayoutConstraint(item: pageViewController.view,
attribute: NSLayoutConstraint.Attribute.centerY,
relatedBy: NSLayoutConstraint.Relation.equal,
toItem: self.pageView,
attribute: NSLayoutConstraint.Attribute.centerY,
multiplier: 1.0,
constant:0),
NSLayoutConstraint(item: pageViewController.view,
attribute: NSLayoutConstraint.Attribute.width,
relatedBy: NSLayoutConstraint.Relation.equal,
toItem: self.pageView ,
attribute: NSLayoutConstraint.Attribute.width,
multiplier: 1.0,
constant: 0),

NSLayoutConstraint(item: pageViewController.view,
attribute: NSLayoutConstraint.Attribute.height,
relatedBy: NSLayoutConstraint.Relation.equal,
toItem: self.pageView,
attribute: NSLayoutConstraint.Attribute.height,
multiplier: 1.0,
constant:0)
])



pageViewController.initialIndex = self.initialIndex
}

private func setupComponent()
{
//selectedBar
let frame = selectedBar.frame
selectedBar.frame = CGRect(x: itemsFrame[0].origin.x, y: frame.origin.y, width:itemsFrame[0].width , height: self.selectedBarHeight)
//selectedBar: iPhoneXR(height=896)で、デフォルトheight=3なので、割合計算を行う。
selectedBar.frame = CGRect(x: itemsFrame[0].origin.x,
y: self.collectionView.frame.origin.y + self.collectionView.frame.height + 1,
width:itemsFrame[0].width,
height: ( 1/896 * self.view.frame.height ) * self.selectedBarHeight)


//Tab
changeUserInteractionEnabled(searchTab: false)
Expand Down Expand Up @@ -303,12 +356,18 @@ open class PolioPagerViewController: UIViewController, TabCellDelegate, PolioPag
self.setSearchTab()


self.items.forEach{item in
for i in 0...self.items.count-1
{
let item = self.items[i]
var width: CGFloat
let fontSize = ( 1/414 * self.view.frame.width ) * self.items[i].font.pointSize
//フォントサイズをXRに合わせて計算し直す

self.items[i].font = UIFont(name: self.items[i].font.fontName, size: fontSize)!

if let _ = item.image
{
width = item.cellWidth == nil ? 50 : item.cellWidth!
width = item.cellWidth == nil ? defaultCellHeight : item.cellWidth!
}
else
{
Expand Down Expand Up @@ -400,8 +459,9 @@ extension PolioPagerViewController: UICollectionViewDataSource, UICollectionView
tabCell.imageView.image = image
tabCell.titleLabel.isHidden = true
}
else if let title = items[index].title {
else if let title = items[index].title{
tabCell.titleLabel.text = title
tabCell.titleLabel.font = items[index].font
}

print(tabCell)
Expand All @@ -413,7 +473,7 @@ extension PolioPagerViewController: UICollectionViewDataSource, UICollectionView
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = itemsWidths[indexPath.row]

return CGSize(width: width, height: 50)
return CGSize(width: width, height: defaultCellHeight)
}

}
72 changes: 38 additions & 34 deletions PolioPager/PolioPagerViewController.xib
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,11 @@
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="CvP-VG-H8J">
<rect key="frame" x="0.0" y="88" width="253" height="3"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
<integer key="value" value="5"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<containerView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Wzf-PR-459">
<rect key="frame" x="0.0" y="92" width="414" height="804"/>
<constraints>
<constraint firstAttribute="width" secondItem="Wzf-PR-459" secondAttribute="height" multiplier="69:134" id="dzX-7F-u2P"/>
</constraints>
<rect key="frame" x="0.0" y="83" width="414" height="813"/>
</containerView>
<collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" scrollEnabled="NO" dataMode="none" translatesAutoresizingMaskIntoConstraints="NO" id="aU5-VK-ObC">
<rect key="frame" x="0.0" y="40" width="414" height="35"/>
<rect key="frame" x="0.0" y="44" width="414" height="35"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<collectionViewFlowLayout key="collectionViewLayout" scrollDirection="horizontal" minimumLineSpacing="10" minimumInteritemSpacing="10" id="QoS-za-uQQ">
<size key="itemSize" width="50" height="50"/>
Expand All @@ -58,17 +45,20 @@
</connections>
</collectionView>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="ypc-Nb-c7o">
<rect key="frame" x="53" y="40" width="361" height="35"/>
<rect key="frame" x="53" y="44" width="361" height="35"/>
<subviews>
<textField opaque="NO" contentMode="scaleToFill" misplaced="YES" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="Search..." textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="xVK-Lg-LpH">
<rect key="frame" x="0.0" y="20" width="288" height="17"/>
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="Search..." textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="xVK-Lg-LpH">
<rect key="frame" x="0.0" y="11" width="288" height="17"/>
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" returnKeyType="done"/>
</textField>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="QIt-qH-8xk">
<rect key="frame" x="296" y="13" width="45" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="QIt-qH-8xk">
<rect key="frame" x="296" y="4.5" width="45" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="2SS-83-Cqv"/>
<constraint firstAttribute="width" constant="45" id="HX4-c1-Uu6"/>
</constraints>
<state key="normal" title="cancel"/>
<connections>
<action selector="tappedCancel:" destination="-1" eventType="touchDown" id="uX6-hH-Sac"/>
Expand All @@ -77,24 +67,38 @@
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="xVK-Lg-LpH" firstAttribute="leading" secondItem="ypc-Nb-c7o" secondAttribute="leading" id="K0e-xq-9PY"/>
<constraint firstItem="xVK-Lg-LpH" firstAttribute="top" secondItem="ypc-Nb-c7o" secondAttribute="top" constant="20" id="T85-lb-OGQ"/>
<constraint firstAttribute="bottom" secondItem="xVK-Lg-LpH" secondAttribute="bottom" constant="-2" id="zHs-ho-azh"/>
<constraint firstItem="xVK-Lg-LpH" firstAttribute="centerY" secondItem="ypc-Nb-c7o" secondAttribute="centerY" constant="2" id="ERU-1G-sRP"/>
<constraint firstAttribute="trailing" secondItem="QIt-qH-8xk" secondAttribute="trailing" constant="20" id="Mai-iZ-Xor"/>
<constraint firstItem="QIt-qH-8xk" firstAttribute="leading" secondItem="xVK-Lg-LpH" secondAttribute="trailing" constant="8" id="PLJ-Kb-IFh"/>
<constraint firstItem="QIt-qH-8xk" firstAttribute="centerY" secondItem="ypc-Nb-c7o" secondAttribute="centerY" constant="2" id="j0k-Ws-Tbe"/>
<constraint firstItem="xVK-Lg-LpH" firstAttribute="leading" secondItem="ypc-Nb-c7o" secondAttribute="leading" id="wfz-qO-nGo"/>
</constraints>
</view>
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="CvP-VG-H8J">
<rect key="frame" x="0.0" y="88" width="253" height="3"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
<integer key="value" value="5"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="Wzf-PR-459" firstAttribute="centerX" secondItem="i5M-Pr-FkT" secondAttribute="centerX" id="HYC-OP-NFe"/>
<constraint firstItem="aU5-VK-ObC" firstAttribute="top" secondItem="ypc-Nb-c7o" secondAttribute="top" id="KfT-vC-BhY"/>
<constraint firstItem="aU5-VK-ObC" firstAttribute="leading" secondItem="i5M-Pr-FkT" secondAttribute="leading" id="Kpb-8k-deo"/>
<constraint firstItem="aU5-VK-ObC" firstAttribute="top" secondItem="i5M-Pr-FkT" secondAttribute="top" constant="40" id="SSg-TA-vR1"/>
<constraint firstItem="aU5-VK-ObC" firstAttribute="trailing" secondItem="Wzf-PR-459" secondAttribute="trailing" id="W1R-Hw-yoG"/>
<constraint firstItem="ypc-Nb-c7o" firstAttribute="leading" secondItem="fnl-2z-Ty3" secondAttribute="leading" constant="53" id="gD7-9p-UUh"/>
<constraint firstItem="ypc-Nb-c7o" firstAttribute="trailing" secondItem="aU5-VK-ObC" secondAttribute="trailing" id="lPC-FQ-mDD"/>
<constraint firstAttribute="bottom" secondItem="Wzf-PR-459" secondAttribute="bottom" id="nh7-NC-RNB"/>
<constraint firstItem="aU5-VK-ObC" firstAttribute="bottom" secondItem="ypc-Nb-c7o" secondAttribute="bottom" id="vOL-nt-VIK"/>
<constraint firstAttribute="trailing" secondItem="aU5-VK-ObC" secondAttribute="trailing" id="xHN-mG-Y0W"/>
<constraint firstItem="Wzf-PR-459" firstAttribute="trailing" secondItem="fnl-2z-Ty3" secondAttribute="trailing" id="1SK-mT-adb"/>
<constraint firstAttribute="bottom" secondItem="Wzf-PR-459" secondAttribute="bottom" id="2qS-yh-lBr"/>
<constraint firstItem="ypc-Nb-c7o" firstAttribute="leading" secondItem="fnl-2z-Ty3" secondAttribute="leading" constant="53" id="4XM-Fk-RYl"/>
<constraint firstItem="aU5-VK-ObC" firstAttribute="leading" secondItem="fnl-2z-Ty3" secondAttribute="leading" id="UC7-Aj-VP0"/>
<constraint firstItem="ypc-Nb-c7o" firstAttribute="trailing" secondItem="fnl-2z-Ty3" secondAttribute="trailing" id="ZWH-wQ-KBD"/>
<constraint firstItem="ypc-Nb-c7o" firstAttribute="top" secondItem="fnl-2z-Ty3" secondAttribute="top" id="Zom-cB-VMO"/>
<constraint firstItem="Wzf-PR-459" firstAttribute="top" secondItem="aU5-VK-ObC" secondAttribute="bottom" constant="4" id="g3I-6H-dp2"/>
<constraint firstItem="aU5-VK-ObC" firstAttribute="bottom" secondItem="ypc-Nb-c7o" secondAttribute="bottom" id="lRn-Ut-fb7"/>
<constraint firstItem="Wzf-PR-459" firstAttribute="leading" secondItem="fnl-2z-Ty3" secondAttribute="leading" id="pAc-Qw-Oes"/>
<constraint firstItem="aU5-VK-ObC" firstAttribute="height" secondItem="i5M-Pr-FkT" secondAttribute="height" multiplier="0.0390625" id="rQI-Ux-WwE"/>
<constraint firstItem="aU5-VK-ObC" firstAttribute="trailing" secondItem="fnl-2z-Ty3" secondAttribute="trailing" id="sMx-mA-cbh"/>
<constraint firstItem="aU5-VK-ObC" firstAttribute="top" secondItem="fnl-2z-Ty3" secondAttribute="top" id="zaK-Hg-hbA"/>
</constraints>
<viewLayoutGuide key="safeArea" id="fnl-2z-Ty3"/>
<point key="canvasLocation" x="47.826086956521742" y="35.491071428571423"/>
Expand Down
4 changes: 2 additions & 2 deletions PolioPager/TabCell.xib
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<nil key="highlightedColor"/>
</label>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="N3Q-ss-jXm">
<rect key="frame" x="18" y="20" width="14" height="14"/>
<rect key="frame" x="18" y="18" width="14" height="14"/>
<constraints>
<constraint firstAttribute="height" constant="14" id="PBx-An-8zt"/>
<constraint firstAttribute="width" constant="14" id="UUw-Kk-Q0a"/>
Expand All @@ -39,7 +39,7 @@
<constraint firstItem="2S9-i1-ZPg" firstAttribute="centerY" secondItem="gTV-IL-0wX" secondAttribute="centerY" id="QlA-GQ-wcD"/>
<constraint firstItem="2S9-i1-ZPg" firstAttribute="centerX" secondItem="gTV-IL-0wX" secondAttribute="centerX" id="ifz-nR-ppy"/>
<constraint firstItem="N3Q-ss-jXm" firstAttribute="centerX" secondItem="gTV-IL-0wX" secondAttribute="centerX" id="jug-hD-0qB"/>
<constraint firstItem="N3Q-ss-jXm" firstAttribute="centerY" secondItem="gTV-IL-0wX" secondAttribute="centerY" constant="2" id="yFs-Na-CEI"/>
<constraint firstItem="N3Q-ss-jXm" firstAttribute="centerY" secondItem="gTV-IL-0wX" secondAttribute="centerY" id="yFs-Na-CEI"/>
</constraints>
<viewLayoutGuide key="safeArea" id="ZTg-uK-7eu"/>
<connections>
Expand Down

0 comments on commit 4bc0b3b

Please sign in to comment.