I would like to have a keyboard with a non-transparent keyboard - I couldn\'t get this with any of the supported UIKeyboardTypes. Is there another way around this?
I
Apple does not allow anyone to modify the default keyboard. If you are going to be using iOS 7 then you will have to deal with the translucent keyboard.
The only other way I can think of is designing your own keyboard, but that's a tedious process to go through.
I was looking into the same thing today, and I found a simple workaround (although, not yet sure how reliable it is).
In order to work, I've set up an inputAccessoryView
for my keyboard controls (UITextView
and UITextField
). In the UIView
class that I've set as the inputAccessoryView
I added the followings:
-(void)layoutSubviews{
[super layoutSubviews];
frame = _lKeyboardBackground.frame;
frame.origin.y = [self convertPoint:self.frame.origin toView:self.superview].y+self.frame.size.height;
frame.size.width = self.bounds.size.width;
frame.origin.x = 0;
frame.size.height = 500;
_lKeyboardBackground.frame = frame;
[self refreshKeyboardBackground];
}
-(void)didMoveToSuperview{
[super didMoveToSuperview];
if (self.superview) {
[self.superview.layer insertSublayer:_lKeyboardBackground atIndex:lMagicLayerIndex];
}
else {
[_lKeyboardBackground removeFromSuperlayer];
}
}
-(void)setFrame:(CGRect)frame{
[super setFrame:frame];
[self refreshKeyboardBackground]; // setFrame: is called when keyboard changes (e.g: to a custom input view)
}
-(void)refreshKeyboardBackground{
if (_lKeyboardBackground.superlayer) {
CALayer *parent = _lKeyboardBackground.superlayer;
[_lKeyboardBackground removeFromSuperlayer];
[parent insertSublayer:_lKeyboardBackground atIndex:lMagicLayerIndex];
}
}
_lKeyboardBackground
is a CALayer
that I've setup in the init methods:
_lKeyboardBackground = [CALayer layer];
_lKeyboardBackground.backgroundColor = [[UIColor redColor] CGColor]; //or some less annoying color
This should theoretically pass Apple's approval, but I never tested. There's also plenty of changes this will not work in future versions, or in some other situations (e.g when there's a split keyboard on the iPad)
lMagicLayerIndex
that I use is 1, which gives the absolute color.
Note that the blur can still be noticed on key-strokes.
The keyboard in iOS7 is translucent when app is compiled in Xcode 5 with a Base SDK of iOS 7.
If you build the app on Xcode 4.6.x instead, you'll have the non-translucent keyboard as before.
(i know this is a shitty fix but nonetheless, i thought i'd suggest it)
anyways, you could alternatively try making use of the default keyboard notifications:
UIKeyboardWillShowNotification
UIKeyboardWillHideNotification
should go something like:
-(void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
-(void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
-(void)keyboardWillShow:(NSNotification *)note
{
/*
Would have used:
CGRect rectStart = [note.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect rectEnd = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
Reason for not using:
The above two keys are not being used although, ideally, they should have been
since they seem to be buggy when app is in landscape mode
Resolution:
Using the deprecated UIKeyboardBoundsUserInfoKey since it works more efficiently
*/
CGRect rectStart_PROPER = [note.userInfo[UIKeyboardBoundsUserInfoKey] CGRectValue];
rectStart_PROPER.origin.y = self.view.frame.size.height;
UIView *vwUnderlay = [self.view viewWithTag:8080];
if (vwUnderlay) {
[vwUnderlay removeFromSuperview];
}
vwUnderlay = [[UIView alloc] init];
[vwUnderlay setFrame:rectStart_PROPER];
[vwUnderlay setBackgroundColor:[UIColor orangeColor]];
[vwUnderlay setTag:8080];
[self.view addSubview:vwUnderlay];
[UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue]
delay:0
options:[note.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16
animations:^{
[vwUnderlay setFrame:CGRectOffset(vwUnderlay.frame, 0, -vwUnderlay.frame.size.height)];
}
completion:nil];
}
-(void)keyboardWillHide:(NSNotification *)note
{
UIView *vwUnderlay = [self.view viewWithTag:8080];
[UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue]
delay:0
options:[note.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16
animations:^{
[vwUnderlay setFrame:CGRectOffset(vwUnderlay.frame, 0, vwUnderlay.frame.size.height)];
}
completion:^(BOOL finished){
[vwUnderlay removeFromSuperview];
}];
}
Use keyboard notification to show/hide a custom black view behind the keyboard (or white if you use the white keyboard) and voila, transparent no more.