Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 10    Views: 137

PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
04/20/14 06:25 PM (10 years ago)

Yes! A UISwitch that toggles a UITextField widget

Hi all, With some helpful pointers from Chris1, I've set up a UISwitch that toggles the visibility of another UIwidget, in my case a UITextField that I'm using as a search bar. Took a fair bit of fiddling so here it is if anyone can make use of it and it saves you some time. This is all in my BT_viewController but I have used a conditional statement to limit its appearance to just those screens that I want it on. In my case I have the UISwitch in the navBar and the UITextField widget 'hovering' on a BT_map screen, but everything could be moved around depending on where you place it in your viewController (and which viewController you place it in, I guess). ----- In ViewDidLoad ----- //This is just styling the UISwitch and some of these are probably pushing the friendship with Apple. You can ignore these and you'll get the default iOS UISWitch (no need for these next 3 lines if you just want the default). [[UISwitch appearance] setOnTintColor: [UIColor colorWithPatternImage:[UIImage imageNamed:@"green_off"]]]; // this is the LHS background when the switch is on (ie switched to the right) [[UISwitch appearance] setTintColor:[BT_color getColorFromHexString:@"#9eba21"]]; // the starting tint colour [[UISwitch appearance] setThumbTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"switch_icon1"]]]; // the switch button's face; you'll do a lot of fiddling with the image to get this to be where you want it to be on the button face. //Create the switch: UISwitch *navSwitch = [[UISwitch alloc] initWithFrame:(CGRectZero)]; [navSwitch addTarget:self action: @selector(magicSwitch:) forControlEvents:UIControlEventValueChanged]; [self.navSwitch setOn:YES animated:YES]; //Allow the UISwitch to become a navBar button: UIBarButtonItem *citySearch = [[UIBarButtonItem alloc] initWithCustomView:(navSwitch)]; //Load the switch as a navBar button: NSArray *actionButtonItems = @[citySearch]; self.navigationItem.LeftBarButtonItems = actionButtonItems; } // The method that toggles the UITextField. The UITextField 'tf' is hidden by default using [tf setHidden:YES]; in the UITextField widget settings further down. -(IBAction)magicSwitch:(UISwitch *)sender {[BT_debugger showIt:self theMessage:@"magicSwitch pressed"]; if([sender isOn]) { // Show UITextField [tf setHidden:NO]; [BT_debugger showIt:self theMessage:@".. The navSwitch was just turned on"]; } else { // Hide UITextField [tf setHidden:YES]; [BT_debugger showIt:self theMessage:@".. The navSwitch was just turned OFF"]; } } ---- in ViewWillAppear --- //From here down is the styling and settings for the UITextField widget that appears and disappears when the UISwitch is pressed: //Set up a textField "search bar" or whatever you want it to be for your screen: UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(16, 9, 250, 30)]; // x,y,width,height tf.font = [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]; tf.backgroundColor=[UIColor whiteColor]; tf.placeholder=@"Search another city..."; /* An option: together, these next two lines do exactly the same thing as tf.placeholder (above), except that tf.placeholder is NOT customisable. It gives a very light grey text colour, whereas tf.text and tf.textcolor together allow for different text colours. tf.text=@"Search another city..."; tf.textColor = [UIColor blueColor]; */ tf.borderStyle = UITextBorderStyleRoundedRect; tf.adjustsFontSizeToFitWidth = YES; //adjust the font size to fit width. tf.keyboardType = UIKeyboardTypeAlphabet; //keyboard type of your choice tf.returnKeyType = UIReturnKeyDone; //returnKey type for keyboard tf.clearButtonMode = UITextFieldViewModeWhileEditing; //for clear button on right side [tf setHidden:YES]; tf.delegate = self; [self.view addSubview:tf]; self.tf = tf; That's it! A customised on/off switch in the navBar that shows/hides a UITextField widget. Hope it can be of some use to someone. Cheers Paddy.
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
04/20/14 06:34 PM (10 years ago)
Sweet. I'm loving your snippets, Paddy. I can't say thanks enough. Cheers! -- Smug
 
NCbuzz
Code is Art
Profile
Posts: 575
Reg: Sep 11, 2013
Lillington, NC
11,100
like
04/21/14 04:40 PM (10 years ago)
Wow- Thanks Paddy!!! Down the road I know this will be a huge help. Do you have a snapshot of how it turned out on your screen?
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
04/21/14 05:00 PM (10 years ago)
Aw, shucks Smug, shoulders of giants and all that. :) Thanks NCBuzz, I hope you can use it - This is the default map screen, no search bar visible: https://dl.dropboxusercontent.com/u/43652415/Screen%20Shot%202014-04-22%20at%209.41.05%20am.png Here it is with the search bar up: https://dl.dropboxusercontent.com/u/43652415/Screen%20Shot%202014-04-22%20at%209.40.35%20am.png What you can't see in these is the cool core animation that slides the search icon across when it is tapped. It slides because that search icon is actually the switch button (the round one), and the little 'x' that appears next to it when it's on is what would normally be the LHS coloured background in the switch slider. Setting the colour of the slider's RHS background (its "OFF state") has made it disappear into the navBar background as they're the same colour. I still have to work on those search and 'x' images to get the resolution matching the other icons as they look a bit crappy at the moment, but you get the idea. (And now I'm thinking maybe I should ditch this and use a segmented control next to the search field itself... I'm wondering if the search switch is too far away or disjointed from the search bar. UX question... we'll see.) But it's been fun to explore how these things work! Cheers Paddy
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
04/21/14 05:28 PM (10 years ago)
Paddy, that is looking *suuuuuuuuu-weet* !! I *love* that little compass in your upper right corner. Way cool! :) Cheers! -- Smug Edit: I gotta ask, lol! Your 'location' is set in Australia. The project you're working with is in Texas. Client? Relatives? Living in Texas now? Planning to go?
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
04/21/14 05:47 PM (10 years ago)
Hehe, just maintaining the mystery Smug! You're right, I'm in Australia but my app content covers the USA and bits of Canada so far... long story but I used to live up that way. And I just love Austin, it's a great fit for what we're doing with the business behind the app, which is all about sustainable food/eateries. I have it set as my default simulator testing location to remind me to get back there someday soon! But it could be anywhere - I have pins all over the place, 100+ cities from Seattle to Sarasota. (Oh, and the compass was a gift from Kittsy. One line of code, insanely easy, shout out if you want it.) Cheers Paddy.
 
NCbuzz
Code is Art
Profile
Posts: 575
Reg: Sep 11, 2013
Lillington, NC
11,100
like
04/21/14 06:10 PM (10 years ago)
Nice!! I think you are going to need to change that "Lost but trying"- especially with the map ;)
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
04/21/14 06:19 PM (10 years ago)
I'd absolutely love to get that compass! I'll stick in every map that I have, lol! I think it's way cool! :) Cheers! -- Smug
 
CMCOFFEE
Android Fan
Profile
Posts: 2017
Reg: Jan 04, 2013
Amarillo, Texas
26,670
like
04/21/14 06:56 PM (10 years ago)
I agree with NCBuzz and SmugWimp, I learn so much from your posts PaddyO. We need you to make plugins!
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
04/21/14 07:19 PM (10 years ago)
Here's the compass thingy, straight from the horse's mouth: https://www.buzztouch.com/forum/thread.php?tid=40F9D280FFD11017D8E29F3&currentPage=1 (and you're in there anyway Smug!!) Cheers Paddy
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
04/21/14 07:20 PM (10 years ago)
NCBuzz definitely lost, that's why I need the map!
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.