Disabling keys on keyboard - ios

I am new to Objective-C, and I am looking to limit a user from switching from the alphabet portion of a normal keyboard to the numeric/punctuation side. This being said, I would like to disable the button on the keyboard that does the switch. Maybe I'm putting in the wrong search parameters, but I'm finding little on Google and even less in my reference books. How would I go about actually disabling a button on an iOS keyboard? With alpha/numeric/punctuation characters this would be easily done by just ignoring the inputed characters that were entered, but the button that switches between keyboard portions does an action as opposed to returning a character.
Thank you for your help!

You actually can add and remove buttons of the default UIKeyboard
There's some recipes on the Internet like this: http://www.iphonedevsdk.com/forum/iphone-sdk-development/6573-howto-customize-uikeyboard.html and like this: http://www.iphonedevsdk.com/forum/iphone-sdk-development/6275-add-toolbar-top-keyboard.html
Those posts show you how to add a button, however the same principle can be used to remove.
Below I'll show you a compilation of one of the solutions:
//The UIWindow that contains the keyboard view -
//It some situations it will be better to actually
//iterate through each window to figure out where the keyboard is,
// but In my applications case
//I know that the second window has the keyboard so I just reference it directly
//
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows]
// objectAtIndex:1];
//Because we cant get access to the UIKeyboard throught the SDK we will
// just use UIView.
//UIKeyboard is a subclass of UIView anyways
UIView* keyboard;
//Iterate though each view inside of the selected Window
for(int i = 0; i < [tempWindow.subviews count]; i++)
{
//Get a reference of the current view
keyboard = [tempWindow.subviews objectAtIndex:i];
//Check to see if the className of the view we have
//referenced is "UIKeyboard" if so then we found
//the keyboard view that we were looking for
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
{
// Keyboard is now a UIView reference to the
// UIKeyboard we want. From here we can add a subview
// to th keyboard like a new button
//Do what ever you want to do to your keyboard here...
}
}

I implemented a neater solution. The trick is to place a disabled key image on top of the keyboard.
To do this
Run emulator (in 100% scale) and screen grab the asset you'd like (in my case, this was a disabled Done button at the bottom right end)
Place this image on top of the keyboard
Note that keyboard is placed in a separate UIWindow (since iOS5 I believe) and thus, you will need to do the following
+ (void) addSubviewToTop:(UIView *)view {
int count = [[[UIApplication sharedApplication] windows] count];
if(count <= 0) {
warn(#"addSubviewToTop failed to access application windows");
}
UIWindow *top_window = [[[UIApplication sharedApplication] windows] objectAtIndex:count-1];
[top_window addSubview:view];
[top_window bringSubviewToFront:view];
}

I'm not sure if the SDK has changed such that #ppaulojr's answer no longer works, or if I just have things set up weirdly on my system, but with the following tweaks I was able to get it to work!
The posts linked in #ppaulojr's answer are great (http://www.iphonedevsdk.com/forum/iphone-sdk-development/6573-howto-customize-uikeyboard.html and http://www.iphonedevsdk.com/forum/iphone-sdk-development/6275-add-toolbar-top-keyboard.html), and they helped me to get this to work.
Apparently the actual keyboard view is now embedded as a subview in some grander UIKeyboard view structure so a bit of recursion is involved. I got this to work:
-(void) findKeyboard {
NSArray* windows = [[UIApplication sharedApplication] windows];
for (int i = 0; i < [windows count]; i++) {
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows]
objectAtIndex:i];
for(UIView *subView in [tempWindow subviews])
{
[self checkViews:subView];
}
}
}
-(void)checkViews:(UIView *)inView
{
for(UIView *keyboard in inView.subviews)
{
NSLog( #"ViewName: %#", [keyboard description] ); // Which view are we looking at
//Check to see if the className of the view we have
//referenced is "UIKeyboard" if so then we found
//the keyboard view that we were looking for
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
{
// Keyboard is now a UIView reference to the
// UIKeyboard we want. From here we can add a subview
// to th keyboard like a new button
//Do what ever you want to do to your keyboard here...
break;
}
// Recurse if not found
[self checkViews:subView];
}
}
I also found that the best place to call this function is from -(void)textViewDidBeginEditing:(UITextView *)textView like so:
- (void)textViewDidBeginEditing:(UITextView *)textView {
NSLog(#"textViewDidBeginEditing");
[self findKeyboard];
}
This does the keyboard modifications as soon as the keyboard is added to the window, but before it actually shows up, so that the whole time it raises from the bottom, it will have been modified.

Related

Test if object is an instance of class UISegment

So I'm doing some ugly hacks to modify the appearance of individual segments in a UISegmentedControl. When I walk down the UISegmentedControl's subview array and print each immediate subview's class name, I can see there are instances of UISegment for each segment in the control:
for (UIView *view in [[self segments] subviews])
{
NSLog(#"%#", NSStringFromClass([view class]));
}
Now apparently Xcode can't resolve this class, so I can't use the familiar [view isKindOfClass:[UISegment class]].
I assumed I was just missing a header to include, but I also can't find any docs for it on Apple's developer reference site.
So how would I go about testing if an object is an instance of UISegment? And what exactly is this class - whose name can be resolved at run-time but not at compile-time?
edit
What I'm trying to achieve is a clear indication that a particular segment is enabled or disabled. For instance, when I disable an entire UISegmentedControl, the following works great to update both selected and unselected segment colors/fonts:
- (void)setIsEnabled:(BOOL)isEnabled
{
[super setIsEnabled:isEnabled];
// be careful and ensure you use the isEnabled getter because it takes isEnabledForConfiguration into account
[[self segments] setUserInteractionEnabled:[self isEnabled]];
[[self segments] setTintColor:[self interactionColor]];
[[self segments] setTitleTextAttributes:[NSDictionary dictionaryWithObject:[self enabledTextColor]
forKey:NSForegroundColorAttributeName]
forState:UIControlStateNormal];
}
But sometimes only a subset of the segments should be disabled, and I would like to visually make that apparent (the -(void)setEnabled:forSegmentAtIndex: method doesn't seem to have any visible effect).
So I've found that I can modify these UISegment subviews to achieve the visual updates (nasty hack, isn't guaranteed to work in the future, I know...).
To try protecting against those future API changes, I've done my best to implement a flexible way to grab a reference to the UISegment objects by segment index.
First, I grab the title of the segment at the input segment index, then traverse all the way down the UISegmentedControl view hierarchy until I find something that responds to the text selector and whose text value equals the title. That view always seems to be a UISegmentLabel (which is another private class!), and his superview is the UISegment I'm looking for. So I've come up with:
- (UIView *)getSegmentAtIndex:(NSInteger)index
{
NSString *title = [[self segments] titleForSegmentAtIndex:index];
id segment = nil;
if (nil != title && 0 < [[EFBGlobalUtil trimWhitespace:title] length])
{
segment = [[EFBGlobalUtil viewWithCondition:^BOOL(UIView *view)
{
return [view respondsToSelector:#selector(text)] && [title isEqualToString:[view valueForKey:NSStringize(text)]];
}
inView:[self segments]] /* this dude -> */superview];
}
return segment;
}
The question really stems from that superview getter I stuck on the end. I would like to verify the superview is actually a UISegment, but the solution to that will also help me check for UISegmentLabel objects as well (instead of the text selector).
And for reference, here's the +(void)viewWithCondition:inView: utility routine:
+ (UIView *)viewWithCondition:(BOOL (^)(UIView *view))condition inView:(UIView *)view
{
// non-recursive BFS traversal of a view hierarchy until specified UIView matching condition is found
NSMutableArray<UIView *> *stack = [[NSMutableArray alloc] initWithObjects:view, nil];
UIView *curr;
while ([stack count] > 0)
{
curr = [stack firstObject];
[stack removeObject:curr];
if (condition((UIView *)curr))
{
return (UIView *)curr;
}
[stack addObjectsFromArray:[curr subviews]];
}
return nil;
}

UISplitViewController pan to primary view from anywhere

Sorry for the long-winded explination, but this question - or something similar - has been asked a few times and I havent found a satisfactory answer. I am writing an iPad app in iOS 8 that implements UISplitViewController. Recently I have been attempting to get it to work on the iPhone. It transferred over pretty well, everything collapses automatically and a back button is included in the left side of my nav. bar.
My problem is that I want to keep the back button functionality to pop one view off the stack, but also be able to pan back to the primary view even if there are several detail views on top of it. Ideally, I want to be able to overwrite or redirect the interactivePopGestureRecognizer so that the gesture smoothly pans to the primary view (in some cases it can have anywhere from 1 to 4 detail views stacked on top of it). But, I cannot figure out how to do this.
My current solution (code below) is to disable the interactivePopGestureRecognizer in the detail viewcontroller and implement my own ScreenEdgePanGestureRecognizer that, when triggered, executes popToRootViewController. I've subclassed the ScreenEdgePanGestureRecognizer so it treats the screen edge pan as a discrete "swipe" (i.e. once a large enough screen edge swipe is detected - pop everything off the stack so the primary view is visible).
Code in detail view controller to stop interactivePopGestureRecognizer:
-(void)viewWillAppear : (BOOL) animated {
[super viewWillAppear : animated];
// stops navigation controller from responding to the default back swipe gesture
if ([self.navigationController respondsToSelector:#selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled =NO;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
}
// Disable the default back swipe gesture tied to automatically included back button
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if ([gestureRecognizer isEqual:self.navigationController.interactivePopGestureRecognizer]) {
return NO;
} else {
return YES;
}
}
I didn't think it was necessary to include my subclass for the screenEdgePanGestureRecognizer because it has nothing to do with the solution I am asking about here is some pseudocode that shows what my #selector does in the detail viewcontroller:
- (IBAction)leftEdgeSwipe:(ScreenEdgeSwipeGestureRecognizer*)sender {
if (sender.swipeIsValid) {
[(UINavigationController *)self.splitViewController.viewControllers[0]
popToRootViewControllerAnimated:YES];
}
}
I tried to use the continuous pan, but cannot find a way to present the primary view in the background as I am pulling the current view aside to give that clean, smooth panning effect. I am able to make it so I can move the current view around, but there is just a grey background behind it where I would want my primary view to be.
Summation: If there is indeed no way to change the interactivePopGestureRecognizer to always jump to my primary view (ideal solution), then any info on how I can make my own smooth pan back to my primary view would be much appreciated.
So I have been messing around with making a smooth panning gesture subclass. Currently it functions similarly to Apple's back gesture except it jumps all the way back to the root view controller instead of popping one view off the stack. The only problem is that it does not yet show the primary view in the background while panning. I will update the answer once I get that worked out.
Here is the subclass:
#import <UIKit/UIKit.h>
#import <UIKit/UIGestureRecognizerSubclass.h>
#import "ScreenEdgeSwipeGestureRecognizer.h"
#interface ScreenEdgeSwipeGestureRecognizer ()
#property (nonatomic) UINavigationController* navController;
#end
#implementation ScreenEdgeSwipeGestureRecognizer{
CGPoint _screenCenter;
CGPoint _cumulativePanDistance;
}
- (id)initWithNavigationController:(UINavigationController*)navController {
self = [super initWithTarget:self action:#selector(leftEdgePan:)];
_screenCenter = CGPointZero;
_cumulativePanDistance = CGPointZero;
self.edges = UIRectEdgeLeft;
self.navController = navController;
return self;
}
- (IBAction)leftEdgePan:(ScreenEdgeSwipeGestureRecognizer*)sender {
assert(sender == self);
switch (self.state) {
case UIGestureRecognizerStateBegan:
[self initializePositions];
break;
case UIGestureRecognizerStateChanged:
[self updatePositions];
break;
case UIGestureRecognizerStateEnded:
[self animateViewBasedOnCurrentLocation];
break;
case UIGestureRecognizerStateCancelled:
[self animateViewToCenter];
break;
default:
break;
}
// Reset velocity of the pan so current velocity does not compound with velocity of next cycle
[sender setTranslation:CGPointMake(0, 0) inView:sender.view];
}
- (void)initializePositions {
_screenCenter = self.view.center;
_cumulativePanDistance = CGPointZero;
}
- (void)updatePositions {
// Track position of user touch event
CGPoint deltaSinceLastCycle = [self translationInView:self.view];
// View center = view center at last cycle + distance moved by user touch since last cycle
self.view.center=CGPointMake((self.view.center.x + deltaSinceLastCycle.x), self.view.center.y+ 0);
// Update the total positive distance traveled by the user touch event.
_cumulativePanDistance.x = _cumulativePanDistance.x + deltaSinceLastCycle.x;
}
- (void)animateViewBasedOnCurrentLocation {
if (_cumulativePanDistance.x >= (_screenCenter.x - 50)){
[self reset];
[_navController popToRootViewControllerAnimated:YES];
}else{
[self animateViewToCenter];
[self reset];
}
}
- (void)animateViewToCenter {
[UIView animateWithDuration:0.25 animations:^{self.view.center = self->_screenCenter;}];
}
- (void)reset {
[super reset];
_cumulativePanDistance = CGPointZero;
self.state = UIGestureRecognizerStatePossible;
}
#end
Here is how I instantiate the recognizer in my view controller:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// Initialize the screen edge pan gesture recognizer.
_masterNavigationController = self.splitViewController.viewControllers[0];
ScreenEdgePanGestureRecognizer* edgePanRecognizer = [[ScreenEdgeSwipeGestureRecognizer alloc] initWithNavigationController:_masterNavigationController];
// Add recognizer to view this controller is bound to.
[self.view addGestureRecognizer:_edgePanRecognizer];
}

What is my FirstResponder

I created an application and in ViewController.m , viewDidLoad , i wrote following code
for (UIView* v in self.view.subviews){
NSLog(#"View is %#",v);
NSLog(#"First Responder is %#",[v isFirstResponder]?#"YES":#"NO");
}
NSLog(#"First Responder is %#",[self isFirstResponder]?#"YES":#"NO");
NSLog(#"First Responder is %#",[self.view isFirstResponder]?#"YES":#"NO");
But it it returns NO for everything. What is my first responder by default ?
In your main view controller, do this:
for (UIView *sub in self.view.subviews)
{
if (sub.isFirstResponder)
NSLog(#"It's me!");
}
From documentation for OS X:
Determining First-Responder Status
Usually an NSResponder object can always determine if it's currently the first responder by asking its window (or itself, if it's an NSWindow object) for the first responder and then comparing itself to that object. You ask an NSWindow object for the first responder by sending it a firstResponder message. For an NSView object, this comparison would look like the following bit of code:
if ([[self window] firstResponder] == self) {
// do something based upon first-responder status
}
Note: This is for OS X. Unfortunately iOS doesn't have a similar system, but can be achieved via:
UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView* firstResponder = [keyWindow performSelector:#selector(firstResponder)];
This is undocumented, and could be rejected by Apple. For testing and exploratory research, this is handy for you.
Here are some documents on first responders in iOS:
Cocoa Application Competencies for iOS
Event Handling Guide for iOS

Hide/remove buttons from keyboard

Is it possible to access/remove buttons from the keyboard on iPad?
I want the user to have access only to the number keyboard without possibility to switch between other types of keyboard. For this, i need to hide/disable "switch buttons" (buttons with label "ABC").
To access the keyboard i use:
UIWindow * tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i = 0; i < [tempWindow.subviews count]; i++)
{
keyboard = [tempWindow.subviews objectAtIndex:i];
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
{
// access elements of keyboard
}
}
Also, the problem is that [keyboard.subviews count] = 0 , while the keyboard was found.
Each UITextField conforms to text input protocol called UITextInputTraits which declares property keyboardType. You can define the keyboard in interface builder or calling setKeyboardType: method on your text field. The keyboard types are defined in documentation. In your particular case you should use UIKeyboardTypeDecimalPad for number input.

UISearchDisplayController Without Dimming?

I am writing a SplitView iPad app. Inside the DetailViewController, there's a little view that contains a UITableView and a UISearchBar and its controller. This view does not represent the whole screen space reserved for the DetailViewController. Actually, it uses just half of it. There's an UIImageView on the other half.
And this is where trouble comes in: every time I use the search bar, the displaycontroller (I assume) dims everything present inside the DetailViewController, including the image view. That is not consistent with what someone would expect when running the app. Is there any way to set the frame to be dimmed? Or at least disable dimming for good?
Thanks in advance.
You are correct that it is the UISearchDisplayController that is managing the "dimming" effect that you're seeing.
What the UISearchDisplayController is doing is adding a UIControl as a subview to the view of the searchContentsController (a property of UISearchDisplayController), which is likely your detail-view controller. This UIControl is just an alpha'd view with a gray background. It seems to have a touch-up-inside event handler that ends searching when tapped.
To constrain the dimming effect to your sub-view of the detail-view, you need to do three things. (I'm assuming your detail-view-controller is defined via a xib. If not, these steps can be done in code too.)
1) add a new UIViewController to your detail-view-controller xib. Attach this new view-controller to an IBOutlet of your detail-view-controller. In my example I call this "_searchAreaViewController". This is important, even if you wont ever access the view controller (but remember, you'll have to release it at some point)
#interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate, UITableViewDelegate, UITableViewDataSource> {
UIPopoverController *popoverController;
UIToolbar *toolbar;
id detailItem;
UILabel *detailDescriptionLabel;
IBOutlet UIViewController* _searchAreaViewController;
}
2) make the containing view for your search area the view of this new view-controller. To do this, use Interface Builder to set a new referencing outlet for this view by dragging the outlet to the searchAreaViewController and selecting the "view" outlet. You must have a containing view - it should be a subview of your detail-view, and it should contain the UISearchBar and likely your UITableView.
3) make the searchContentsController property of the UISearchDisplayController refer to this new view controller instead of the detail-view-controller. This can only be done via Interface Builder as the property is read-only (IB has some magic to make this work?) If you need to do this step via code you'll have to subclass the UISearchDisplayController and return the correct value from a property override of "searchContentsController".
I made a sample app to demonstrate this and the only line of code I had to add to the SplitView template was the one listed in step 1 above. Everything else was just adding the views/controllers and connecting them properly in IB.
good luck!
iOS 8+
[[UIView appearanceWhenContainedInInstancesOfClasses:#[NSClassFromString(#"UISearchDisplayControllerContainerView")]] setHidden:YES];
iOS 7
[View appearanceWhenContainedIn:NSClassFromString(#"UISearchDisplayControllerContainerView"), nil] setHidden:YES];
I know, that UISearchDisplayController is deprecated for now, but if you still need to use it, you can solve your issue with one line of code perfectly. Add it to viewDidLoad method.
Could you clarify what you mean by "use the search bar" and "dims everything present"? I interpret what you wrote in such a way that the keyboard pops up when you are about to enter text in the text field of the search bar. And that at this point the detail view is dimmed out, preventing user interaction.
The cause is that the search bar implements a modal dialog which prevents user interaction with the view as long as the keyboard is shown. Unfortunately, there doesn't seem to be any way to configure the search bar to prevent this behavior. On the other hand I am not sure that the user won't expect this behavior since search bars are modal consistently and behave like this in general under iOS.
I have tried two work-arounds:
1.) There is a property of the UIViewController called modalPresentationStyle which produces exactly the behavior you describe if it has the value UIModalPresentationFormSheet ("All uncovered areas are dimmed to prevent the user from interacting with them.", see the Apple documentation). But setting this property to a different values does not change the result (at least for me it didn't work).
2.) You would need to write your own non-modal search bar replacement since a standard UITextField is non-modal and thus does not dim out any other UI elements. This approach works, but you might need a little more work to make it look like a "regular" search bar. But, again, since this search bar behaves differently from the modal normal search bars in iOS this might not really be what the users expect.
I know I am late and this is a horrible idea here, but 'setHidden:No' did not work for me.
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
BOOL hasBeenremoved = NO;
hasBeenremoved = [[[[NSThread mainThread] threadDictionary] objectForKey:#"hasBeenremoved"] boolValue];
if (hasBeenremoved)
{
UIView* dimmingView = nil;
dimmingView = [[[NSThread mainThread] threadDictionary] objectForKey:#"dimmingView"];
UIView* dimmingViewSuperView = nil;
dimmingViewSuperView = [[[NSThread mainThread] threadDictionary] objectForKey:#"dimmingViewSuperView"];
[dimmingViewSuperView addSubview:dimmingView];
[[[NSThread mainThread] threadDictionary] setObject:#NO forKey:#"hasBeenremoved"];
}
if ([searchText length] == 0 || [searchText isEqualToString:#""] )
{
[searchBar becomeFirstResponder];
[[[self primarySearchDisplayController] searchResultsTableView] reloadData];
[[[self primarySearchDisplayController] searchResultsTableView] setHidden:NO];
for( UIView *subview in self.view.subviews )
{
if([subview isMemberOfClass:[UIControl class]] ||
([[[subview class] description] isEqualToString:#"UISearchDisplayControllerContainerView"]))
{
for(UIView *subView2 in subview.subviews)
{
for(UIView *subView3 in subView2.subviews)
{
if (subView3.alpha < 1)
{
if ([[[subView3 class] description] isEqualToString:#"_UISearchDisplayControllerDimmingView"])
{
[[[NSThread mainThread] threadDictionary] setObject:subView3 forKey:#"dimmingView"];
[[[NSThread mainThread] threadDictionary] setObject:subView3.superview forKey:#"dimmingViewSuperView"];
[[[NSThread mainThread] threadDictionary] setObject:#YES forKey:#"hasBeenremoved"];
[subView3 removeFromSuperview];
}
}
}
}
}
}
}
}

Resources