Skip to content
This repository has been archived by the owner on Sep 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #14 from ermalkaleci/master
Browse files Browse the repository at this point in the history
Merging
  • Loading branch information
ermalkaleci committed Apr 25, 2015
2 parents 12fec9b + 9f5a50e commit e231d34
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 97 deletions.
105 changes: 34 additions & 71 deletions CarbonKit/CarbonTabSwipeNavigation.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ @interface CarbonTabSwipeNavigation() <UIPageViewControllerDelegate, UIPageViewC
UISegmentedControl *segmentController;
UIImageView *indicator;

NSLayoutConstraint *tabTopConstraint;
NSLayoutConstraint *indicatorLeftConst;
NSLayoutConstraint *indicatorWidthConst;
}
Expand Down Expand Up @@ -85,25 +84,12 @@ - (instancetype)createWithRootViewController:(UIViewController *)viewController

// add page controller as child
[self addChildViewController:pageController];

// set page controller frame
CGRect pageRect = viewController.view.frame;
pageRect.origin.y = 44;
pageRect.size.height -= 44;
pageController.view.frame = pageRect;
[self.view addSubview:pageController.view];

// finish adding page controller as child
[pageController didMoveToParentViewController:self];

// add self as child to parent
[rootViewController addChildViewController:self];

// set self.view frame
self.view.frame = rootViewController.view.frame;
[rootViewController.view addSubview:self.view];

// finish adding self as a child to parent
[self didMoveToParentViewController:rootViewController];

// create segment control
Expand Down Expand Up @@ -170,7 +156,7 @@ - (instancetype)createWithRootViewController:(UIViewController *)viewController
segmentController.frame = segmentRect;

// create scrollview
tabScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 44)];
tabScrollView = [[UIScrollView alloc] init];
[self.view addSubview:tabScrollView];

// create indicator
Expand All @@ -187,54 +173,25 @@ - (instancetype)createWithRootViewController:(UIViewController *)viewController
[tabScrollView setShowsVerticalScrollIndicator:NO];
[tabScrollView setTranslatesAutoresizingMaskIntoConstraints:NO];

if (viewController.navigationController) {
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tabScrollView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0]];
} else {
CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
tabTopConstraint = [NSLayoutConstraint constraintWithItem:tabScrollView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:statusBarHeight];
[self.view addConstraint:tabTopConstraint];
}

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tabScrollView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tabScrollView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tabScrollView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:0
constant:44]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tabScrollView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0]];
[pageController.view setTranslatesAutoresizingMaskIntoConstraints: NO];
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];

// create constraints
UIView *parentView = self.view;
UIView *pageControllerView = pageController.view;
id<UILayoutSupport> rootTopLayoutGuide = rootViewController.topLayoutGuide;
id<UILayoutSupport> rootBottomLayoutGuide = rootViewController.bottomLayoutGuide;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(rootTopLayoutGuide, rootBottomLayoutGuide, parentView, tabScrollView, pageControllerView);
NSDictionary *metricsDictionary = @{
@"tabScrollViewHeight" : @44
};

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[tabScrollView(==tabScrollViewHeight)][pageControllerView]|" options:0 metrics:metricsDictionary views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tabScrollView]|" options:0 metrics:metricsDictionary views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[pageControllerView]|" options:0 metrics:metricsDictionary views:viewsDictionary]];

[rootViewController.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[rootTopLayoutGuide][parentView][rootBottomLayoutGuide]" options:0 metrics:metricsDictionary views:viewsDictionary]];
[rootViewController.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[parentView]|" options:0 metrics:metricsDictionary views:viewsDictionary]];

[indicator setTranslatesAutoresizingMaskIntoConstraints:NO];
[segmentController addConstraint:[NSLayoutConstraint constraintWithItem:indicator
Expand Down Expand Up @@ -373,12 +330,17 @@ - (void)viewDidLoad {
id viewController = [self.delegate tabSwipeNavigation:self viewControllerAtIndex:selectedIndex];
[viewControllers setObject:viewController forKey:[NSNumber numberWithInteger:selectedIndex]];

[pageController setViewControllers:@[viewController] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

// call delegate
if ([self.delegate respondsToSelector:@selector(tabSwipeNavigation:didMoveAtIndex:)]) {
[self.delegate tabSwipeNavigation:self didMoveAtIndex:selectedIndex];
}
__weak __typeof__(self) weakSelf = self;
[pageController setViewControllers:@[viewController]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:^(BOOL finished) {
__strong __typeof__(self) strongSelf = weakSelf;
// call delegate
if ([strongSelf->_delegate respondsToSelector:@selector(tabSwipeNavigation:didMoveAtIndex:)]) {
[strongSelf->_delegate tabSwipeNavigation:strongSelf didMoveAtIndex:strongSelf->selectedIndex];
}
}];
}

- (void)viewDidAppear:(BOOL)animated {
Expand All @@ -398,13 +360,14 @@ - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceO

- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
[tabTopConstraint setConstant:statusBarHeight];

UIView *tab = tabs[segmentController.selectedSegmentIndex];
indicatorWidthConst.constant = tab.frame.size.width;
indicatorLeftConst.constant = tab.frame.origin.x;

// keep the page controller's width in sync
pageController.view.frame = CGRectMake(pageController.view.frame.origin.x, pageController.view.frame.origin.y, self.view.bounds.size.width, pageController.view.frame.size.height);

[self resizeTabs];
[self fixOffset];
[self.view layoutIfNeeded];
Expand Down
2 changes: 1 addition & 1 deletion Examples/CarbonKitExamples/HomeViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ - (UIViewController *)tabSwipeNavigation:(CarbonTabSwipeNavigation *)tabSwipe vi

// optional
- (void)tabSwipeNavigation:(CarbonTabSwipeNavigation *)tabSwipe didMoveAtIndex:(NSInteger)index {
NSLog(@"%d", index);
NSLog(@"Current tab: %d", (int)index);
}

@end
30 changes: 5 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,40 +73,20 @@ pod install
NSArray *names = @[@"CATEGORIES", @"HOME", @"TOP PAID", @"TOP FREE", @"TOP GROSSING", @"TOP NEW PAID", @"TOP NEW FREE", @"TRENDING"];
UIColor *color = [UIColor colorWithRed:0.753 green:0.224 blue:0.169 alpha:1];
tabSwipe = [[CarbonTabSwipeNavigation alloc] createWithRootViewController:self tabNames:names tintColor:color delegate:self];
[tabSwipe setNormalColor:[UIColor colorWithWhite:1 alpha:0.8]];
[tabSwipe setSelectedColor:[UIColor whiteColor]];
[tabSwipe setNormalColor:[UIColor colorWithWhite:1 alpha:0.8]]; //default
[tabSwipe setSelectedColor:[UIColor whiteColor]]; //default
}
// delegate
- (UIViewController *)tabSwipeNavigation:(CarbonTabSwipeNavigation *)tabSwipe viewControllerAtIndex:(NSUInteger)index {
return nil; // return viewController
return nil; // return viewController at index
}
@end
```

# CONTRIBUTORS
- Ermal Kaleci
[Contributors](https://github.com/ermalkaleci/CarbonKit/graphs/contributors)

# LICENSE
The MIT License (MIT)

Copyright (c) 2015 Ermal Kaleci

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
[The MIT License (MIT)](https://github.com/ermalkaleci/CarbonKit/blob/master/LICENSE)

0 comments on commit e231d34

Please sign in to comment.