Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

like ios8 UIBarButtonItem on ios7 iPad #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions SplitViewControllerDemo/Images.xcassets/img.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x",
"filename" : "img.png"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions SplitViewControllerDemo/SelectColorTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class SelectColorTableViewController: UITableViewController {
if let selectedRowIndexPath = tableView.indexPathForSelectedRow() {
let color = colors[selectedRowIndexPath.row]
colorViewController.color = color
// :-)
UIApplication.sharedApplication().keyWindow?.tintColor = color.color
}
}
}
Expand Down
94 changes: 91 additions & 3 deletions SplitViewControllerDemo/SplitViewSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,94 @@
import UIKit

// MARK: - iPad ios7support
class CustomView: UIView {

let imgView = UIImageView()
let label = UILabel()

init(from: UIBarButtonItem?) {

// chevron
imgView.image = UISplitViewController.ios7Support.img?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
imgView.sizeToFit()
imgView.frame.offset(dx: 0, dy: 1)

// label
label.text = " "
if let title = from?.title {
label.text! += title
}
label.sizeToFit()
label.frame.offset(dx: 0, dy: 1)

// init with proper size
super.init(frame: label.bounds)

// set colors
label.textColor = tintColor

// compose
addSubview(imgView)
addSubview(label)

// functionality
let action: Selector = from?.action ?? Selector()

// tap animation
let tap = UITapGestureRecognizer(target: self, action: Selector("tapAction"))

// UIBarButtonItem current functionality
if let target = from?.target as? UISplitViewController {
tap.addTarget(target, action: action)
}
addGestureRecognizer(tap)
}

func tapAction() {
// experimental, I am not able to get 'system default value'
label.alpha = 0.2

UIView.animateWithDuration(0.3) {
[unowned self] in
self.label.alpha = 1
}
}

override init(frame: CGRect) {
super.init(frame: frame)
}

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override func tintColorDidChange() {
label.textColor = tintColor
super.tintColorDidChange()
}
}

class ModeButtonItem: UIBarButtonItem {

init(from: UIBarButtonItem?) {
super.init(customView: CustomView(from: from))
}

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override init() {
super.init()
}

}

extension UISplitViewController: UISplitViewControllerDelegate {

struct ios7Support {
static var modeButtonItem: UIBarButtonItem?
static var img = UIImage(named: "img.png")
}

var backBarButtonItem: UIBarButtonItem? {
Expand All @@ -26,8 +109,11 @@ extension UISplitViewController: UISplitViewControllerDelegate {
}
}
set {
ios7Support.modeButtonItem = newValue
}
if let button = newValue {
ios7Support.modeButtonItem = ModeButtonItem(from: newValue)
} else {
ios7Support.modeButtonItem = nil
} }
}

// simple trick, without swizzling :-)
Expand All @@ -39,8 +125,10 @@ extension UISplitViewController: UISplitViewControllerDelegate {
public func splitViewController(svc: UISplitViewController, willHideViewController aViewController: UIViewController, withBarButtonItem barButtonItem: UIBarButtonItem, forPopoverController pc: UIPopoverController) {
if (!svc.respondsToSelector(Selector("displayModeButtonItem"))) {
if let detailView = svc.viewControllers[svc.viewControllers.count-1] as? UINavigationController {
// set the button
svc.backBarButtonItem = barButtonItem
detailView.topViewController.navigationItem.leftBarButtonItem = barButtonItem
// get NEW!!!!! button
detailView.topViewController.navigationItem.leftBarButtonItem = svc.backBarButtonItem
}
}
}
Expand Down