Code Monkey home page Code Monkey logo

dznsegmentedcontrol's Introduction

DZNSegmentedControl

License Pod Version Carthage compatible BuddyBuild

A drop-in replacement for UISegmentedControl for showing counts, images and many other useful features.

Demo Gif

Features

  • Fully customizable control with tint color, font, sizes and animation duration.
  • Animated and width auto-adjusting selection indicator.
  • UIBarPositioning support.
  • UIAppearance support.
  • UIScrollView extension for scroll offset binding.

Installation

Available in CocoaPods

pod 'DZNSegmentedControl'

How to use

For complete documentation, visit CocoaPods' auto-generated doc

Step 1

Import "DZNSegmentedControl.h"

Step 2

Creating a new instance of DZNSegmentedControl is very similar to what you would do with UISegmentedControl:

NSArray *items = @[@"Tweets", @"Following", @"Followers"];
    
DZNSegmentedControl *control = [[DZNSegmentedControl alloc] initWithItems:items];
control.tintColor = [UIColor blueColor];
control.delegate = self;
control.selectedSegmentIndex = 1;
    
[control addTarget:self action:@selector(selectedSegment:) forControlEvents:UIControlEventValueChanged];

You can additionally set more properties:

[control setCount:@(12) forSegmentAtIndex:0];
[control setTitle:@"Hello" forSegmentAtIndex:1];
[control setEnabled:NO forSegmentAtIndex:2];

Sample project

Take a look into the sample project. Everything is there.

License

(The MIT License)

Copyright (c) 2015 Ignacio Romero Zurbuchen [email protected]

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.

dznsegmentedcontrol's People

Contributors

dev-xdyang avatar fangqiuming avatar howelse avatar jeffreyjackson avatar ndmeiri avatar nebuto avatar neowinston avatar readmecritic avatar xhacker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dznsegmentedcontrol's Issues

Move `isImageMode` to header file

Should we move isImageMode property to header file so that we could make this property to YES and do more further configurations?

Observer never remove

  • (void)setSegmentedControl:(DZNSegmentedControl *)segmentedControl
    {
    objc_setAssociatedObject(self, &segmentedControlKey, segmentedControl, OBJC_ASSOCIATION_ASSIGN);

    if (segmentedControl) {
    [self addObserver:self forKeyPath:contentOffsetKey options:NSKeyValueObservingOptionNew context:&observerContext];
    [segmentedControl addTarget:self action:@selector(dzn_didChangeSegement:) forControlEvents:UIControlEventValueChanged];
    }
    else if (self.segmentedControl) {
    [self removeObserver:self forKeyPath:contentOffsetKey context:&observerContext];
    [segmentedControl removeTarget:self action:@selector(dzn_didChangeSegement:) forControlEvents:UIControlEventValueChanged];
    }

    self.scrollOnSegmentChange = YES;
    }


else if (self.segmentedControl) {


self.segmentedControl always return nil

How to detect second click on DZNSegmentedControl?

Assuming I have a DZNSegmentedControl with previous and next, in my case I should be able to click next more than one time, the default behaviour of DZNSegmentedControl will not recognise the success if second click is on same segment. So how to deal with that?

Crash when deallocating

I am trying to use this Control on a Storyboard. My view controller is arranged this way:
UIViewController > UIView (Main) > UIView (Gradient BG) > UIView (DZNSegmentedControl)

If I push that view controller and then pop it, the app crashes with this log message (note: zombies enabled):

*** -[UIView release]: message sent to deallocated instance 0x7fedeae64ac0

If I remove the class name from that view, the app will no longer crash.
If the view assigned as DZNSegmentedControl is not inside a second view (the gradient), but is inside the main view, it also will not crash.

I created a sample project that demonstrates this.
The project has a second branch called programmatically, when I keep the inner view set as a regular UIView and then add a DZNSegmentedControl inside it programmatically. that way the app will not crash.

Pod version bump

Hi, @dzenbot Why did not you update the version of Pod after fixing some bugs? Please update the version for the current master if it is necessary.

"setItems"- set methods in DZNSegmentedControl.m

Set Methods should not be called in itself setting.
This may crash when setting it another time.

- (void)setItems:(NSArray *)items
{
    if (_items) {
        [self removeAllSegments];
#pragma mark This should not be called this way.
//        [self setItems:nil];
    }

    if (items) {
        _items = [NSArray arrayWithArray:items];
    }

    [self configure];
}

It's crash point could be here:
By the way, I'd like to change buttons to newButtons for safety.

- (NSArray *)buttons
{
//--- This Line
    NSMutableArray *newButtons = [NSMutableArray new];
//---
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            [newButtons addObject:view];
        }
    }
    return newButtons;
}

update podspec

Hi, can we get a new podspec update for the current master?

Support for custom NSNumberFormatter

There are a couple ways this could be implemented.

  • An @property on each instance of DZNSegmentedControl
  • An optional delegate method that asks for a custom NSNumberFormatter for each segment index. This would allow, for example, number values to be displayed in one segment and currency values in another segment.

Which do you think would be best?

ios 10 beta

Hi
i tried to run my app in xcode beta 6 & ios 10 simulator (iphone 5)
DZNSegmentedControl doesn't show right

Make the control work in a UIView

Is it possible to make it work in a UIView? So I can fix it at the top, and when the UITableView is scrolled the segment view will be still visible.

I tried but couldn't get it to work.

when items is not in english, setSelectedSegmentIndex will has no effect

when i init control as follow

 _timeControl = [[DZNSegmentedControl alloc] initWithItems:@[NSLocalizedString(@"日榜", @""),
                                                                    NSLocalizedString(@"周榜", @""),
                                                                    NSLocalizedString(@"总榜", @"")]];
_timeControl.tintColor=FZLightColor;

when calling

 [self.timeControl setSelectedSegmentIndex:2 animated:YES];

segmentIndex's tintColor is not FZLightColor

when i init control as follow

_timeControl = [[DZNSegmentedControl alloc] initWithItems:@[NSLocalizedString(@"day", @""),
                                                                    NSLocalizedString(@"week", @""),
                                                                    NSLocalizedString(@"all", @"")]];
_timeControl.tintColor=FZLightColor;

that will effect when calling setSelectedSegmentIndex:animated

Use a separate colour for indicator

Is it possible to use a particular colour for the indicator, and another for the text? In my case the indicator colour needs to be really bright, which won't work for text.

setHairLineColor not working

I'm trying to set the hairline color with [UIColor colorWithRed:142/255.0 green:8/255.0 blue:35/255.0 alpha:1.0];
This doesn't change the color at all.

NumberOfLines for segmented title

Currently the segmented title is clipped when it is longer than width even though they have height to expand due to a fixed set on numberOfLines = 1 (showsCount: false, I don't want to show count).

Is there a way to make this changes without forking out? The button in the segmentViews are private variables.

heigh issue after the last update

Dears,

im adding the DZNSegmentedControl as a subview, the height was ok, filling the whole parent view. but after the last, the segmented is cut into half to set the font height.

screen shot 2014-11-23 at 9 15 56 am

setting the height explicitly like this
self.segmentedControl.height = self.segmentedView.frame.size.height;
is causing the segnemted not filling the whole view again after rotation

screen shot 2014-11-23 at 9 17 07 am
screen shot 2014-11-23 at 9 17 17 am

thank you

DZNSegmentedControl setSelectionText Color

Hi, is there a way for me to set the text color when i select the segment?
The text color dimmed when I used

    func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [AnyObject], transitionCompleted completed: Bool) {
        segmentSubviews.selectedSegmentIndex = myPageViewController.viewControllers[0].pageIndex
    }

screen shot 2015-06-24 at 9 32 36 pm

iOS 8

I've upgraded an app to IOS8 and DZNSegmented Control now crashes

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM setObject:atIndex:]: index 2 beyond bounds for empty array'

-[__NSArrayM setObject:atIndex:] + 458
-[NSMutableArray setObject:atIndexedSubscript:] + 48
-[DZNSegmentedControl setCount:forSegmentAtIndex:] + 649

It seems that self.counts[segment] = count; in - (void)setCount:(NSNumber *)count forSegmentAtIndex:(NSUInteger)segment is the culprit as the array is init but has a count of 0.

I suggest adding changing - (void)setItems:(NSArray *)items to this:

- (void)setItems:(NSArray *)items
{
    if (_items) {
        [self removeAllSegments];
    }

    if (items) {
        _items = [NSArray arrayWithArray:items];

        if (self.counts)
            self.counts = nil;

        self.counts = [[NSMutableArray alloc] initWithCapacity:items.count];
        for (int i = 0; i < items.count; i++) {
            [self.counts addObject:@(i)];
        }

        [self insertAllSegments];
    }
}

-[ViewController positionForBar:]: unrecognized selector sent to instance error

Hi,
i have tried to use your project in my app, in viewDidLoad i do this:

NSArray *items = @[@"Tweets", @"Following", @"Followers"];

DZNSegmentedControl *control = [[DZNSegmentedControl alloc] initWithItems:items];
control.tintColor = [UIColor blueColor];
control.delegate = self;
control.selectedSegmentIndex = 1;

but the app crash when set the delegate, and give me this error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '-[ViewController positionForBar:]: unrecognized selector sent to instance

how i can solve?

Potential pod version problem?

Adding this line to my podfile

pod 'DZNSegmentedControl', '1.2'

causes this error when pod install analyzes dependencies.

[!] Unable to satisfy the following requirements:

- `DZNSegmentedControl (= 1.2)` required by `Podfile`

I understand the meaning of the error but wasn't the pod updated to version 1.2?

Support font sizing / Deal with clipping

I'm trying to set the font to a smaller size so that the entire string will fit into the control. Currently, if you run off the edge of the provided area, the text just gets cutoff.

It would be great if you could either change the font size manually, or do some multi-line adjustment if the text is going to run outside the bounds.

Should delegate methods be favored for segment attributes?

Right now, there are no delegate methods; the control is given the attributes before it needs them. So far this works fine. But as more attributes are supported it might become verbose to call
-set[Attribute]:forSegmentIndex:
for every attribute on every segment. What are your thoughts? (Not trying to criticize your implementation, which I think works well right now. Just something to consider for the future.)

Support for using without counts

It might be useful to use this segmented control without any count labels.
@charles-rouchon has a decent modification about this. You should send a PR, allowing counts and not.

First UITableView row is hidden on view load

Hey,

since 1.3.1 I have the issue, that the first row of the UITableView is hidden behind the DZNSegmentedControl.
After switching between the groups the first row is visible.

I add the header to the tableView after reloadData

DZNSegmentedControl in Interface Builder

Hey,

As usual great work :) I was wondering if we can setup a UISegmentedControl in IB and have it conform to the DZNSegmentedControl class? I tried it I can't set its class in IB

Tall selection indicator covers segment title

I'm trying to use a tall selection indicator as shown in the graphic below. However, when a segment is selected, the indicator stays behind the titles but then covers them after the animation completes. I tried commenting out the line [self bringSubviewToFront:_selectionIndicator]; in -layoutSubviews but this did not fix the problem.

covering selection indicator

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.