Skip to content

Commit

Permalink
iOS 6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
graetzer committed Sep 21, 2012
1 parent c72dad2 commit 43ef554
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 65 deletions.
9 changes: 5 additions & 4 deletions Demo/SGTabs/SGViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ - (void)viewDidLoad
[super viewDidLoad];

self.webView.delegate = self;
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 400.0, 25.0)];
self.webView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];

self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 400.0, 30.0)];
self.textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.textField.backgroundColor = [UIColor whiteColor];
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.text = @"http://www.google.com";
self.textField.clearButtonMode = UITextFieldViewModeAlways;
self.textField.keyboardType = UIKeyboardTypeURL;
Expand All @@ -50,16 +53,14 @@ - (void)viewDidLoad

UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
UIBarButtonItem *space2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
UIBarButtonItem *reload = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self action:@selector(reload:)];
UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(add:)];


self.toolbarItems = [NSArray arrayWithObjects:space,urlBar,space2,reload,add,nil];
self.toolbarItems = [NSArray arrayWithObjects:space,urlBar,space,reload,add,nil];
[SGURLProtocol registerProtocol];
}

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# SGTabs

Tab component for iOS 5+. You can show your ViewControllers in tabs, it is possible to add and remove tabs on the fly.
Tab component for iOS 5 and iOS 6. You can show your ViewControllers in tabs, it is possible to add and remove tabs on the fly.

# Features
- Uses iOS 5 UIViewController Container API
- Add and remove tabs on the fly with animations
- User can remove tabs, if you allow it.
- Dynamically show and hide a UIToolbar at the top
- Show the UIBarButtonItems in viewController.toolbarItems in the toolbar
- Show the UIBarButtonItems in viewController.toolbarItems property in the built in toolbar
- Enables you to build mobile Safari style Apps. (See demo)

# Demo
Expand Down
10 changes: 4 additions & 6 deletions Source/SGTabView.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//
// Copyright (c) 2012 Simon Grätzer
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand All @@ -24,13 +24,11 @@
#import <UIKit/UIKit.h>
#import <CoreText/CoreText.h>

@interface SGTabView : UIView {
CGSize _tSize;
CGFloat _cap;
}
@interface SGTabView : UIView

@property (nonatomic, readonly, strong) UIButton *closeButton;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) UIButton *closeButton;
@property (assign, nonatomic) BOOL selected;

- (id)initWithFrame:(CGRect)frame title:(NSString *)title;

Expand Down
43 changes: 20 additions & 23 deletions Source/SGTabView.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//
// Copyright (c) 2012 Simon Grätzer
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand All @@ -31,9 +31,10 @@ @interface SGTabView ()
@property (nonatomic, strong) UIColor *tabDarkerColor;
@end

@implementation SGTabView
@synthesize titleLabel, closeButton;
@synthesize tabColor;
@implementation SGTabView {
CGSize _tSize;
CGFloat _cap;
}
@dynamic title;

- (id)initWithFrame:(CGRect)frame title:(NSString *)title
Expand All @@ -55,24 +56,30 @@ - (id)initWithFrame:(CGRect)frame title:(NSString *)title
self.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0];
self.titleLabel.minimumFontSize = 14.0;
self.titleLabel.textColor = [UIColor darkGrayColor];
self.titleLabel.shadowColor = [UIColor colorWithWhite:0.6 alpha:0.5];
self.titleLabel.shadowOffset = CGSizeMake(0, 0.5);
self.title = title;
[self addSubview:self.titleLabel];

self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
_closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.closeButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;

[self.closeButton setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[self.closeButton setTitle:@"x" forState:UIControlStateNormal];
[self.closeButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.closeButton setShowsTouchWhenHighlighted:YES];
self.closeButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:17.0];
[self addSubview:self.closeButton];
[self addSubview:self.closeButton];
}
return self;
}

- (void)setTitle:(NSString *)title {
self.titleLabel.text = title;
_tSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font];
}

- (NSString *)title {
return self.titleLabel.text;
}

- (void)layoutSubviews {
CGRect b = self.bounds;
CGFloat margin = kCornerRadius;
Expand All @@ -81,7 +88,7 @@ - (void)layoutSubviews {
if (t.width > b.size.width*0.75) {
t.width = b.size.width*0.75 - 2*margin;
}

if(!self.closeButton.hidden) {
self.titleLabel.frame = CGRectMake((b.size.width - t.width)/2 + margin,
(b.size.height - t.height)/2,
Expand All @@ -95,15 +102,6 @@ - (void)layoutSubviews {
self.closeButton.frame = CGRectMake(margin, 0, 25, b.size.height);
}

- (void)setTitle:(NSString *)title {
self.titleLabel.text = title;
_tSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font];
}

- (NSString *)title {
return self.titleLabel.text;
}

- (void)drawRect:(CGRect)rect {
CGRect tabRect = self.bounds;
CGFloat tabLeft = tabRect.origin.x;
Expand All @@ -129,19 +127,18 @@ - (void)drawRect:(CGRect)rect {
// Top rigth
CGPathAddArc(path, NULL, tabRight, tabTop + kCornerRadius, kCornerRadius, M_PI, -M_PI_2, NO);
CGPathAddLineToPoint(path, NULL, tabRight, tabTop);
CGPathAddLineToPoint(path, NULL, tabLeft, tabTop);
CGPathCloseSubpath(path);

CGContextRef ctx = UIGraphicsGetCurrentContext();

// Fill with current tab color
CGColorRef startColor = self.alpha < 1. ? self.tabDarkerColor.CGColor : self.tabColor.CGColor;
CGColorRef startColor = self.selected ? self.tabColor.CGColor : self.tabDarkerColor.CGColor;

CGContextSaveGState(ctx);
CGContextAddPath(ctx, path);
CGContextSetFillColorWithColor(ctx, startColor);
CGContextSetShadow(ctx, CGSizeMake(0, -1), kShadowRadius);
CGContextAddPath(ctx, path);
CGContextFillPath(ctx);
CGContextRestoreGState(ctx);

CGPathRelease(path);

Expand Down
19 changes: 9 additions & 10 deletions Source/SGTabsView.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//
// Copyright (c) 2012 Simon Grätzer
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -61,24 +61,23 @@ - (void)addTab:(NSString *)title {
// Float the subview in from rigth
CGRect frame = CGRectMake(self.bounds.size.width, 0, width, self.bounds.size.height - kTabsBottomMargin);
SGTabView *newTab = [[SGTabView alloc] initWithFrame:frame title:title];
newTab.alpha = 0.9;
newTab.closeButton.hidden = YES;

// Setup gesture recognizers
UITapGestureRecognizer *tapG = [[UITapGestureRecognizer alloc] initWithTarget:self
UITapGestureRecognizer *tapG = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleTap:)];
tapG.numberOfTapsRequired = 1;
tapG.numberOfTouchesRequired = 1;
tapG.delegate = self;
[newTab addGestureRecognizer:tapG];

UIPanGestureRecognizer *panG = [[UIPanGestureRecognizer alloc] initWithTarget:self
UIPanGestureRecognizer *panG = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(handlePan:)];
panG.delegate = self;
[newTab addGestureRecognizer:panG];

// Setup close button
[newTab.closeButton addTarget:self action:@selector(handleRemove:) forControlEvents:UIControlEventTouchCancel];
UIControlEvents event = [[UIDevice currentDevice].systemVersion doubleValue] < 6 ? UIControlEventTouchCancel : UIControlEventTouchUpInside;
[newTab.closeButton addTarget:self action:@selector(handleRemove:) forControlEvents:event];

// Add the tab
[self.tabs addObject:newTab];
Expand Down Expand Up @@ -113,12 +112,12 @@ - (void)setSelected:(NSUInteger)selected {
tab.closeButton.hidden = NO;
}

tab.alpha = 1.0;
tab.selected = YES;
[tab setNeedsLayout];
[self bringSubviewToFront:tab];
} else {
tab.closeButton.hidden = YES;
tab.alpha = 0.9;
tab.selected = NO;
[tab setNeedsLayout];
}
[tab setNeedsDisplay];
Expand All @@ -141,7 +140,7 @@ - (void)resizeTabs {
tab.frame = CGRectMake(width*i, 0, width, self.bounds.size.height - kTabsBottomMargin);
}
}

#pragma mark - Actions

- (IBAction)handleRemove:(id)sender {
Expand All @@ -156,7 +155,7 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecogni
return NO;
}

- (void)handleTap:(UITapGestureRecognizer *)sender {
- (void)handleTap:(UITapGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
SGTabView *tab = (SGTabView *)sender.view;
[self.tabsController showIndex:[self.tabs indexOfObject:tab]];
Expand Down
5 changes: 1 addition & 4 deletions Source/SGTabsViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@

@class SGToolbar, SGTabsView;

@interface SGTabsViewController : UIViewController {
CGRect _contentFrame;
BOOL _toobarVisible;
}
@interface SGTabsViewController : UIViewController

/// Is an optional delegate
@property (nonatomic, weak) id<SGTabsViewControllerDelegate> delegate;
Expand Down
34 changes: 18 additions & 16 deletions Source/SGTabsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ - (void)removeViewController:(UIViewController *)viewController index:(NSUIntege

@end

@implementation SGTabsViewController
@implementation SGTabsViewController {
BOOL _toobarVisible;
}

@synthesize delegate;
@synthesize tabContents = _tabContents, currentViewController = _currentViewController;
@synthesize headerView = _headerView, tabsView = _tabsView, toolbar = _toolbar;
@synthesize contentFrame = _contentFrame;

- (id)init {
if (self = [super initWithNibName:nil bundle:nil]) {
Expand All @@ -58,12 +60,10 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
CGRect head = self.headerView.frame;
CGRect bounds = self.view.bounds;
_contentFrame = CGRectMake(bounds.origin.x,
bounds.origin.y + head.size.height,
bounds.size.width,
bounds.size.height - head.size.height);
self.currentViewController.view.frame = self.contentFrame;
}

- (void)viewWillAppear:(BOOL)animated {
self.currentViewController.view.frame = self.contentFrame;
}

Expand All @@ -87,15 +87,9 @@ - (void)loadView {
frame = CGRectMake(head.origin.x, kTabsToolbarHeigth, head.size.width, kTabsHeigth);
_tabsView = [[SGTabsView alloc] initWithFrame:frame];


[self.headerView addSubview:_toolbar];
[self.headerView addSubview:_tabsView];
[self.headerView addSubview:_toolbar];
[self.view addSubview:self.headerView];

_contentFrame = CGRectMake(bounds.origin.x,
bounds.origin.y + head.size.height,
bounds.size.width,
bounds.size.height - head.size.height);
}

- (void)viewDidLoad {
Expand All @@ -108,6 +102,15 @@ - (void)viewDidUnload {
self.tabsView = nil;
}

- (CGRect)contentFrame {
CGRect head = self.headerView.frame;
CGRect bounds = self.view.bounds;
return CGRectMake(bounds.origin.x,
bounds.origin.y + head.size.height,
bounds.size.width,
bounds.size.height - head.size.height);
}

#pragma mark - Tab stuff

- (void)addTab:(UIViewController *)viewController {
Expand Down Expand Up @@ -254,7 +257,6 @@ - (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animated {
head.size.height = toolbarHeight+kTabsHeigth;
toolbar.size.height = toolbarHeight;
tabs.origin.y = toolbarHeight;
_contentFrame.origin.y = head.size.height;

[UIView animateWithDuration:animated ? kToolbarDuration : 0
animations:^{
Expand Down

0 comments on commit 43ef554

Please sign in to comment.