Giter Club home page Giter Club logo

Comments (16)

youknowone avatar youknowone commented on June 19, 2024

Thank you! Looks great and license seems compatible too!

from ui7kit.

KieranLafferty avatar KieranLafferty commented on June 19, 2024

Hey thats hilarious I came here specifically to offer this to be used in your project. Glad to see someone beat me to it!

from ui7kit.

youknowone avatar youknowone commented on June 19, 2024

@KieranLafferty Hi. I wish there were like or +1 button for your comment. Thanks for good switch implementation ;)

from ui7kit.

KieranLafferty avatar KieranLafferty commented on June 19, 2024

I forked the repo and got my control to display in place of the UISwitch using your FoundationExtender, I was wondering, how can I make my KLSwitch coding compliant with UISwitch such that it receives the 'onTintColor' and 'on' properties that are specific to UISwitch, any thoughts?

What I tried so far was to manually decode the coder passed to initWithCoder using keys looked up in the storyboard file

#define kCodingOnKey @"on"
#define kCodingOnTintColorKey @"onColor"
#define kCodingOnColorKey @"onTintColor"    //Not implemented
#define kCodingTintColorKey @"tintColor"
#define kCodingThumbTintColorKey @"thumbTintColor"
#define kCodingOnImageKey @"onImage"
#define kCodingOffImageKey @"offImage"

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [super encodeWithCoder: aCoder];

    [aCoder encodeBool: _on
                forKey: kCodingOnKey];

    [aCoder encodeObject: _onTintColor
                  forKey: kCodingOnTintColorKey];

    [aCoder encodeObject: _tintColor
                  forKey: kCodingTintColorKey];

    [aCoder encodeObject: _thumbTintColor
                  forKey: kCodingThumbTintColorKey];

    [aCoder encodeObject: _onImage
                  forKey: kCodingOnImageKey];

    [aCoder encodeObject: _offImage
                  forKey: kCodingOffImageKey];
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder: aDecoder]) {

        _on = [aDecoder decodeBoolForKey:kCodingOnKey];
        _onTintColor = [aDecoder decodeObjectForKey: kCodingOnTintColorKey];
        _tintColor = [aDecoder decodeObjectForKey: kCodingTintColorKey];
        _thumbTintColor = [aDecoder decodeObjectForKey: kCodingThumbTintColorKey];
        _onImage = [aDecoder decodeObjectForKey: kCodingOnImageKey];
        _offImage = [aDecoder decodeObjectForKey: kCodingOffImageKey];
        _onTintColor = [aDecoder decodeObjectForKey: kCodingOnTintColorKey];

        [self configureSwitch];

    }
    return self;
}

I received the decoder keys for UISwitch using https://github.com/nicklockwood/AutoCoding to list all the NSCoder keys for the class. Here is the list

onColor,
onTintColor,
tintColor,
thumbTintColor,
onImage,
offImage,
on,
enabledAlpha,
enabled,
selected,
highlighted,
contentVerticalAlignment,
contentHorizontalAlignment,
skipsSubviewEnumeration,
viewTraversalMark,
viewDelegate,
monitorsSubtree,
backgroundColorSystemColorName,
gesturesEnabled,
deliversTouchesForGesturesToSuperview,
userInteractionEnabled,
tag,
layer,
"_boundsWidthVariable",
"_boundsHeightVariable",
"_minXVariable",
"_minYVariable",
"_internalConstraints",
"_dependentConstraints",
"_constraintsExceptingSubviewAutoresizingConstraints",
"_shouldArchiveUIAppearanceTags",
restorationIdentifier

My concern is that decoding the NIB may cause apps to get rejected by the App store for doing some shady things.

from ui7kit.

KieranLafferty avatar KieranLafferty commented on June 19, 2024

Also, not able to retrieve values from the NSCoder passed from the storyboard

from ui7kit.

youknowone avatar youknowone commented on June 19, 2024

Actually, I don't know about these issues very well either.

As you see, my basic strategy about this problems in UI7Kit is subclassing to retrieve values from xib/storyboard. (Just to enable users can edit values on Interface builder)

I guess most of values are accessible after initWithCoder: done. And we can do another tricks to hide original UISwitch in awakeFromNib, if needed.

Sorry about unuseful comment. I believe there are each tricks to patch each controls. I will try your switch after basic works.

from ui7kit.

KieranLafferty avatar KieranLafferty commented on June 19, 2024

Yea the issue is that the switch inherits from UIControl not from UISwitch. As such the two classes are siblings and KLSwitch does not automatically decode the switch.

The alternative would be to inherit from UISwitch and make use of the decoding functionality of UISwitch while hiding the UISwitch view layer. However, this is a pretty hacky solution which would use much more memory for very little functional gain.

The best solution would still be to decode them from the NSCoder as that has the details of the runtime vars from the UIStoryboard. Just need to find out why the keys arent workign currently :S

from ui7kit.

youknowone avatar youknowone commented on June 19, 2024

I agree that would be bad idea for KLSwitch :)

UI7Kit is intending both a dependent toolkit and drop-in replacement of UIKit. If I don't inherit UISwitch, people cannot place UISwitch on interface builder and select new switch class. This must be confusing exception. So I still need to subclass UISwitch.

Do you know any other clever solution about this?

from ui7kit.

KieranLafferty avatar KieranLafferty commented on June 19, 2024

What I did in the ui7switch was to use the uo7switch to decode the KlSwitch with the UISwitch decoder passed from storyboard. Sounds confusing but basically it's like this
(On phone so excuse bad formatting)

-(id) initwithcoder:(nscoder*) adecoder {
self = [[KLSwitch alloc] initwithcoder: adecoder];
If(self){...}

Return self;
}

So basically this allows it to receive all of the storyboard coded attributes for uicontrol (shared parent of UISwitch and KlSwitch) but does not receive the attributes specific to UISwitch (on, tintcolor, etc)

Also this sort of feels weird doing it this way there must be a better way

from ui7kit.

youknowone avatar youknowone commented on June 19, 2024

Great. Because I failed similar trick with UIButton, I thought initWithCoder: should return the pointer what it receive. I must did something other wrong before.

I am trying to override UIButton from UIControl

How did you decode your values?
I have no time to test more at this time, but I confirmed storyboard uses decodeXXXForKey: too. So it must work. I will do this more in the night.

I tried:

    self = [UIControl methodImplementationForSelector:@selector(initWithCoder:)](self, _cmd, aDecoder);

And got method calls of:

206989 + UIControl NSObject methodImplementationForSelector:                           
206990 - UIControl UIControl initWithCoder:                                            
206991 - UIView UIView initWithCoder:                                                  
206992 - UIResponder NSObject init 
206993 - UINibDecoder UINibDecoder containsValueForKey:                                
206994 - UINibStringIDTable UINibStringIDTable lookupKey:identifier:                   
206995 - __NSCFConstantString __NSCFString hash                                        
206996 - UINibDecoder UINibDecoder decodeCGRectForKey:                                 
206997 - UINibDecoder UINibDecoder decodeArrayOfCGFloats:count:forKey:                 
206998 - UINibDecoder UINibDecoder decodeBytesForKey:returnedLength:                   
206999 - UINibStringIDTable UINibStringIDTable lookupKey:identifier:                   
207000 - __NSCFConstantString __NSCFString hash                                        
207001 - UINibDecoder UINibDecoder decodeCGPointForKey:                                
207002 - UINibDecoder UINibDecoder decodeArrayOfCGFloats:count:forKey:                 
207003 - UINibDecoder UINibDecoder decodeBytesForKey:returnedLength:                   
207004 - UINibStringIDTable UINibStringIDTable lookupKey:identifier:                   
207005 - __NSCFConstantString __NSCFString hash    
...

from ui7kit.

youknowone avatar youknowone commented on June 19, 2024

I found I get 'reason: 'This coder requires that replaced objects be returned from initWithCoder:'' sometimes. And not sometimes. I am confused.

from ui7kit.

youknowone avatar youknowone commented on June 19, 2024

Tried 2 approaches:

  1. I successed to write KLSwitch as subclass of UISwitch, except a problem that size is fixed to iOS6 UISwitch size.
  2. I failed to object_setClass to replace UI7Switch to KLSwitch. It works on iOS7, but not on iOS6

from ui7kit.

KieranLafferty avatar KieranLafferty commented on June 19, 2024

So I updated my fork of this to work well with KLSwitch

Things that work:

  1. Grabs decoded values from UISwitch by instantiating a UISwitch with the coder then grabbing properties and setting them on the KLSwitch
  2. Constrains the frame size to the height of the UISwitch and the standard proportion of the iOS 7 Switch

Files changes
UI7Switch.m
podfile (had to specify 5.0 in order for compatibility with KLSwitch

TODO:
Grab values from IB without having to create a dummy switch to grab the IB configured properties unique to the switch

Let me know if you want me to create a pull request for this

from ui7kit.

KieranLafferty avatar KieranLafferty commented on June 19, 2024

https://github.com/kieranlafferty/UI7Kit

from ui7kit.

youknowone avatar youknowone commented on June 19, 2024

What a genius work :D Please send me a pull request. I will be appreciated very much.

from ui7kit.

youknowone avatar youknowone commented on June 19, 2024

@KieranLafferty Long time passed, but I found proper key to decode values from interface builder. They are UISwitchOn, UISwitchOnTintColor and UISwitchThumbTintColor

from ui7kit.

Related Issues (20)

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.