浏览代码

header label offset from center

nayooti 5 年之前
父节点
当前提交
28129ea346
共有 2 个文件被更改,包括 25 次插入6 次删除
  1. 4 4
      deltachat-ios/Controller/GalleryViewController.swift
  2. 21 2
      deltachat-ios/View/GallerySectionHeader.swift

+ 4 - 4
deltachat-ios/Controller/GalleryViewController.swift

@@ -141,14 +141,14 @@ extension GalleryViewController: UICollectionViewDataSource, UICollectionViewDel
         return UICollectionReusableView()
         return UICollectionReusableView()
     }
     }
 
 
+    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
+        return CGSize(width: collectionView.frame.width - 2 * gridInsets, height: 36)
+    }
+
     func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
     func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
         let msgId = gridSections[indexPath.section].msgIds[indexPath.row]
         let msgId = gridSections[indexPath.section].msgIds[indexPath.row]
         showPreview(msgId: msgId)
         showPreview(msgId: msgId)
     }
     }
-
-    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
-        return CGSize(width: collectionView.frame.width - 2 * gridInsets, height: 32)
-    }
 }
 }
 
 
 // MARK: - update layout
 // MARK: - update layout

+ 21 - 2
deltachat-ios/View/GallerySectionHeader.swift

@@ -10,6 +10,14 @@ class GalleryGridSectionHeader: UICollectionReusableView {
         return label
         return label
     }()
     }()
 
 
+    private lazy var labelTopConstraint: NSLayoutConstraint = {
+        return label.topAnchor.constraint(equalTo: topAnchor, constant: 0)
+    }()
+
+    private lazy var labelBottomConstraint: NSLayoutConstraint = {
+        return label.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0)
+    }()
+
     var leadingMargin: CGFloat = 0 {
     var leadingMargin: CGFloat = 0 {
         didSet {
         didSet {
             setNeedsLayout()
             setNeedsLayout()
@@ -19,6 +27,7 @@ class GalleryGridSectionHeader: UICollectionReusableView {
     var text: String? {
     var text: String? {
         set {
         set {
             label.text = newValue?.uppercased()
             label.text = newValue?.uppercased()
+            verticalAlignLabel(ratio: 0.75)
         }
         }
         get {
         get {
             return label.text
             return label.text
@@ -38,9 +47,19 @@ class GalleryGridSectionHeader: UICollectionReusableView {
     private func setupSubviews() {
     private func setupSubviews() {
         addSubview(label)
         addSubview(label)
         label.translatesAutoresizingMaskIntoConstraints = false
         label.translatesAutoresizingMaskIntoConstraints = false
+        labelTopConstraint.isActive = true
+        labelBottomConstraint.isActive = true
         label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: leadingMargin).isActive = true
         label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: leadingMargin).isActive = true
-        label.topAnchor.constraint(equalTo: topAnchor, constant: 0).isActive = true
         label.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -leadingMargin).isActive = true
         label.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -leadingMargin).isActive = true
-        label.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0).isActive = true
+    }
+
+    private func verticalAlignLabel(ratio: CGFloat) {
+        safe_assert(ratio <= 1)
+        let labelHeight = label.intrinsicContentSize.height
+        let verticalMarginTotal = frame.height - labelHeight
+        if verticalMarginTotal > 0 {
+            labelBottomConstraint.constant = verticalMarginTotal * (1 - ratio)
+            labelTopConstraint.constant = verticalMarginTotal * ratio
+        }
     }
     }
 }
 }