فهرست منبع

Better menu tabs

Iñigo Valentin 9 سال پیش
والد
کامیت
eaf90238a1
3فایلهای تغییر یافته به همراه51 افزوده شده و 8 حذف شده
  1. 38 0
      MenuCollectionViewCell.swift
  2. 2 2
      app/Base.lproj/Main.storyboard
  3. 11 6
      app/ViewController.swift

+ 38 - 0
MenuCollectionViewCell.swift

@@ -20,6 +20,8 @@
 
 import UIKit
 
+private let highlightedColor = UIColor(rgb: 0xD8D8D8)
+
 /**
  Extension of UICollectionViewCell for the main menu.
  */
@@ -28,4 +30,40 @@ class MenuCollectionViewCell: UICollectionViewCell {
 	@IBOutlet weak var label: UILabel!
  
 	@IBOutlet weak var bar: UIView!
+	
+	var shouldTintBackgroundWhenSelected = true // You can change default value
+	var specialHighlightedArea: UIView?
+	
+	override var isHighlighted: Bool { // make lightgray background show immediately
+		willSet {
+			onSelected(newValue)
+		}
+	}
+	override var isSelected: Bool { // keep lightGray background until unselected
+		willSet {
+			onSelected(newValue)
+		}
+	}
+	func onSelected(_ newValue: Bool) {
+		//self.bar.backgroundColor = UIColor(red: 90/255, green: 180/255, blue: 255/255, alpha: 1)
+		//self.label.font = UIFont.boldSystemFont(ofSize: self.label.font.pointSize)
+		guard selectedBackgroundView == nil else { return }
+		if newValue == true {
+			//contentView.backgroundColor = newValue ? highlightedColor : UIColor.clear
+			self.bar.backgroundColor = UIColor(red: 148/255, green: 209/255, blue: 255/255, alpha: 1)
+			self.label.font = UIFont.boldSystemFont(ofSize: self.label.font.pointSize)
+			self.label.textColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1)
+		}
+		else{
+			self.bar.backgroundColor = UIColor(red: 90/255, green: 180/255, blue: 255/255, alpha: 1)
+			self.label.font = UIFont.systemFont(ofSize: self.label.font.pointSize)
+			self.label.textColor = UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha: 1)
+		}
+	}
+}
+
+extension UIColor {
+	convenience init(rgb: Int, alpha: CGFloat = 1.0) {
+		self.init(red: CGFloat((rgb & 0xFF0000) >> 16) / 255.0, green: CGFloat((rgb & 0xFF00) >> 8) / 255.0, blue: CGFloat(rgb & 0xFF) / 255.0, alpha: alpha)
+	}
 }

+ 2 - 2
app/Base.lproj/Main.storyboard

@@ -85,12 +85,12 @@
                                                         <constraint firstAttribute="width" constant="100" id="eKL-ga-53F"/>
                                                     </constraints>
                                                     <fontDescription key="fontDescription" type="system" pointSize="17"/>
-                                                    <color key="textColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
+                                                    <color key="textColor" red="0.78431372549019607" green="0.78431372549019607" blue="0.78431372549019607" alpha="1" colorSpace="calibratedRGB"/>
                                                     <nil key="highlightedColor"/>
                                                 </label>
                                                 <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="ZNu-9N-o6K">
                                                     <rect key="frame" x="4" y="38" width="92" height="4"/>
-                                                    <color key="backgroundColor" red="0.80000000000000004" green="0.80000000000000004" blue="1" alpha="1" colorSpace="calibratedRGB"/>
+                                                    <color key="backgroundColor" red="0.3529411764705882" green="0.70588235294117641" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                                                     <constraints>
                                                         <constraint firstAttribute="height" constant="4" id="Si5-HX-bjR"/>
                                                     </constraints>

+ 11 - 6
app/ViewController.swift

@@ -347,16 +347,18 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 		cell.label.text = self.items[indexPath.item]
 		
 		//Mark the first cell as selected...
-		if indexPath.item == selected{
-			cell.bar.backgroundColor = UIColor(red: 90/255, green: 180/255, blue: 255/255, alpha: 1)
+		/*if indexPath.item == selected{
+			cell.bar.backgroundColor = UIColor(red: 148/255, green: 209/255, blue: 255/255, alpha: 1)
 			cell.label.font = UIFont.boldSystemFont(ofSize: cell.label.font.pointSize)
+			cell.label.textColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1)
 		}
 			
 		//... and the others as unselected
 		else{
-			cell.bar.backgroundColor = UIColor(red: 148/255, green: 209/255, blue: 255/255, alpha: 1)
+			cell.bar.backgroundColor = UIColor(red: 90/255, green: 180/255, blue: 255/255, alpha: 1)
 			cell.label.font = UIFont.systemFont(ofSize: cell.label.font.pointSize)
-		}
+			cell.label.textColor = UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha: 1)
+		}*/
 		
 		return cell
 	}
@@ -373,9 +375,12 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 	:param: selected Index of the selected item.
 	*/
 	@IBAction func showComponent(selected: Int) {
+		NSLog(":CONTROLLER:DEBUG: Selected: \(selected)")
 		//Activate label
 		var i = 0
-		for cell in sectionCollection.visibleCells as! [MenuCollectionViewCell]{ //TODO: Not only visibles!
+
+		
+		/*for cell in sectionCollection.visibleCells as! [MenuCollectionViewCell]{ //TODO: Not only visibles!
 			if i == selected + 1{
 				cell.bar.backgroundColor = UIColor(red: 90/255, green: 180/255, blue: 255/255, alpha: 1)
 				cell.label.font = UIFont.boldSystemFont(ofSize: cell.label.font.pointSize)
@@ -386,7 +391,7 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 				
 			}
 			i = i + 1
-		}
+		}*/
 		
 		//Show the view
 		if selected == 0 {