Custom View above a keyboard not working in ios9 - ios

Have added a custom view consisting of buttons on top of the keyboard. The buttons are being displayed correctly but on tapping on the buttons, the underlying keys of the keyboard are pressed instead of the button actions.
UIWindow* tempWindow = [UIApplication sharedApplication].windows.lastObject;
for (UIView *keyboard in [tempWindow subviews]) {
if ([[keyboard description] hasPrefix : #"<UIInputSetContainerView"]) {
for(int i = 0 ; i < [keyboard.subviews count] ; i++)
{
UIView* hostkeyboard = [keyboard.subviews objectAtIndex:i];
if([[hostkeyboard description] hasPrefix:#"<UIInputSetHost"] == YES){
[hostkeyboard addSubview:extraRow];
[hostkeyboard bringSubviewToFront:extraRow];
}
}
}
}
extraRow is the UIView consisting of buttons.
Is there anything missing ?

You could do just add a custom view to the keyboard as an inputAccessoryView. Then it just assign the button the correct target you want it to fire when clicked on
- (void)textFieldDidBeginEditing:(UITextField *)textField {
UIView * theView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
[theView setBackgroundColor: [UIColor greyColor]];
UITextField * txtTest = [[UITextField alloc] initWithFrame:CGRectMake(0, 5, theView.frame.size.width, 60)];
UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 65, 100, 30)];
[btn setTitle:#"Click my Button" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(alert:) forControlEvents:UIControlEventTouchUpInside];
// Just put this to see the items or customize the color
[txtTest setBackgroundColor:[UIColor whiteColor]];
[btn setBackgroundColor:[UIColor blueColor]];
// Need this to add it to the view
[theView addSubview:txtTest];
[theView addSubview:btn];
textField.inputAccessoryView = theView;
}

One thing you might want to consider is setting the extraRow's exclusiveTouch property to YES. As a disclaimer I haven't tried this example myself but I think the problem you are having is due to the view passing along the touch event.

Related

Can't touch UIButton in UIScrollView

I am programmatically creating buttons in a view inside UIScrollView. I also have programmatically created UITextField. I can select and enter data into the text fields all day long. But I cannot get any sign the buttons are being touched. I can add them to the main view and they work. But when added to the view inside the UIScrollView, they are lost in another dimension. I've read plenty of info about setting userInteraction, delayContentTouches, and intercepting via gestureRecognizer. The last might be related to my problem. I return NO via [touch.view isDescendantOfView:self.myScrollViewName]. I can't get it to trigger via [touch.view isKindOfClass:[UIButton class]]. Which makes me think I have a more fundamental error I am missing?
related:
https://stackoverflow.com/a/6825839/4050489
edited to add button code per request:
self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.myButton.frame = myButtonFrame;
self.myButton.enabled = interface.isActive;
self.myButton.userInteractionEnabled = YES;
self.myButton.exclusiveTouch = YES;
self.myButton.backgroundColor = [UIColor greenColor];
//self.myButton. = YES;
self.myButton.layer.borderWidth=1.0f;
self.myButton.layer.borderColor=[[UIColor blackColor] CGColor];
self.myButton.layer.cornerRadius = 10;
[self.myButton setTag:interface.tag];
[self.myButton setTitle:interface.Name forState:UIControlStateNormal];
[self.myButton addTarget:self action:#selector(navigatePartButtons:) forControlEvents:UIControlEventTouchUpInside];
[self.myButton setEnabled:YES]; //error check
[self.partSetupView addSubview:self.myButton];
partSetupView is a UIView in the UIScrollView.
Try this sample Code:
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *myScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, 300, 300)];
myScroll.backgroundColor = [UIColor redColor];
[self.view addSubview:myScroll];
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 200)];
myView.backgroundColor = [UIColor yellowColor];
[myScroll addSubview:myView];
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 300, 100)];
myButton.backgroundColor = [UIColor blackColor];
[myView addSubview:myButton];
[myButton addTarget:self action:#selector(btnClick) forControlEvents:UIControlEventTouchDown];
}
-(void)btnClick{
NSLog(#"Button Click");
}
#end

iOS: How to add swipe to delete button to a custom view in viewForHeaderInSection

There is a expand-collapse like table view with custom header using viewForHeaderInSection.
In that, I want to add swipe gesture functionality to delete the same view i.e section header.
My code for viewForHeaderInSection is,
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
mView = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 110)]autorelease];
mView.backgroundColor=[UIColor whiteColor];
[mView setBackgroundColor:[UIColor whiteColor]];
UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 290, 28)];
title.text=[[updateDataArray objectAtIndex:section] objectForKey:#"message"];
title.font = [UIFont fontWithName:#"Raleway-Medium" size:18];
UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
[bt setFrame:CGRectMake(0, 0, 320, 44)];
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[bt setTag:section];
addCellFlag=2;
[bt.titleLabel setFont:[UIFont systemFontOfSize:20]];
[bt.titleLabel setTextAlignment:NSTextAlignmentCenter];
[bt.titleLabel setTextColor:[UIColor blueColor]];
[bt setBackgroundColor:[UIColor whiteColor]];
[bt addTarget:self action:#selector(addCell:) forControlEvents:UIControlEventTouchUpInside];
[mView addSubview:bt];
if (section<updateDataArray.count-1) {
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 2, 320, 0)];
lineView.backgroundColor=[UIColor lightGrayColor];
[bt addSubview:lineView];
[lineView release];
}
mView.backgroundColor=[UIColor clearColor];
[mView addSubview:title];
return mView;
}
Please suggest how to create a swipe to delete button functionality like table view row? I want that button in section header as well.
Add this lines in viewForHeaderInSection before returning your view:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
....
// Your code here
....
mView.tag = section;
UISwipeGestureRecognizer* sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(headerViewSwiped:)];
[sgr setDirection:UISwipeGestureRecognizerDirectionRight]; // change direction accordingly
[mView addGestureRecognizer:sgr];
return mView;
}
- (void)headerViewSwiped:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
UIView *mView= (UIView *)gestureRecognizer.view;
// use mView.tag property to get the section index which you require to delete from array
// update your sectionDataSource array remove all the objects from section array
// so that it will reflect to numberOfSection method
// then remove all the rows which that section containing from your rowDataSource array
// so that it will reflect to numberOfRowsInSection method
// now call [tableView reloadData] method
}
}
This is a base idea to achieve what you required, change this code as per your project requirements.
Hope this helps.
Here are codes DMSLidingCell and TISwipeableTableView. And here is way how to make your own one. make-swipeable-table-view-cell-actions-without-going-nuts-scroll-views. Now if you want to implement this to your header or any other view. Just change the tableview cell to your desired view.
you can add UISwipeGestureRecognizer to every HeaderView
Try with following way
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView * viewTemp = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
[viewTemp setBackgroundColor:[UIColor redColor]];
[viewTemp setTag:(section + 100)];
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipedScreen:)];
swipeGesture.direction = (UISwipeGestureRecognizerDirectionLeft );
[viewTemp addGestureRecognizer:swipeGesture];
return viewTemp;
}
- (void)swipedScreen:(UISwipeGestureRecognizer*)gesture
{
UIView * viewTemp = (UIView *) [self.tableView viewWithTag:[gesture.view tag]];
if(viewTemp){
NSLog(#"Do your stuff");
}
}
Add yourView and deleteButton to a container view, add swipe left gesture to the yourView.
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section{
UIView* mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, thisHeaderHeight)];
//your view
YourView* view;
view.frame = CGRectMake(0, 0, self.view.frame.size.width, thisHeaderHeight);
//....your code
//add button
UIButton* _customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_customButton setTitle:NSLocalizedString(#"Delete", nil) forState:UIControlStateNormal];
_customButton.frame = CGRectMake(self.view.frame.size.width - thisButtonWidth, 0, thisButtonWidth, thisHeaderHeight);
[_customButton addTarget:(id)self action:#selector(onPressDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
//add swipe gesture
UISwipeGestureRecognizer* slgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(onSwipeHeader:)];
[slgr setDirection:UISwipeGestureRecognizerDirectionLeft]; // change direction accordingly
[view addGestureRecognizer:slgr];
UISwipeGestureRecognizer* srgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(onSwipeHeader:)];
[srgr setDirection:UISwipeGestureRecognizerDirectionRight]; // change direction accordingly
[view addGestureRecognizer:srgr];
//view covers customButton
[mainView addSubview:customButton];
[mainView addSubview:view];
return mainView;
}
That's swipe gesture function.
-(void)onSwipeHeader:(UISwipeGestureRecognizer *)gr{
if (gr.state == UIGestureRecognizerStateEnded) {
UIView *mView= (UIView *)gr.view;
CGRect newFrame = mView.frame;
if(gr.direction == UISwipeGestureRecognizerDirectionLeft){
newFrame.origin.x = -thisButtonWidth;
}else if(gr.direction == UISwipeGestureRecognizerDirectionRight){
newFrame.origin.x = 0;
}
[UIView animateWithDuration:0.25 animations:^{mView.frame = newFrame;}];
}
}
Finally, write code to performance delete.

UITextField receives touch events even if I place a button on top

I am trying to block any touch event on UITTextField. To do so, I am trying to place a UIView/Button/Label on top of UITexfield.
But on simulator, I can still click at any location inside text field. What is wrong with my code? How can I put UIButton/Lable on top of textfield so that user can not click anywhere inside text field.
Please note I still want Number Keyboard input in the textfield. I just want to disable clicks/cut/copy/paste/select etc.
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 292, 50, 40)];
[self.textField becomeFirstResponder];
[self.textField setKeyboardType:UIKeyboardTypeNumberPad];
self.textField.delegate = self;
[self.view addSubview:self.textField];
UIButton *topViewonTextField = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
topViewonTextField.backgroundColor = [UIColor clearColor];
[topViewonTextField setEnabled:NO];
//topViewonTextField.layer.zPosition = 100;
[self.view addSubview:topViewonTextField];
#define MAIN_SCREEN [UIScreen mainScreen]
#define ScreenWidth [MAIN_SCREEN bounds].size.width
#define ScreenHeight [MAIN_SCREEN bounds].size.height
If you want "block" a textField, tell to it:
texField.userInteractionEnabled = NO;
Big explanations:
// You create your textField, It can't receive events. By code or in Story
self.textField= [[UITextField alloc] initWithFrame:CGRectMake(10, 190, 80, 30)];
self.textField.placeholder = #"hola";
self.textField.userInteractionEnabled = NO;
self.textField.delegate = self;
[self.view addSubview:self.textField];
//Some Button or other event make the textField in editing mode.
UIButton *buttonToEdit = [[UIButton alloc] initWithFrame:CGRectMake(250, 250, 80, 80)];
[buttonToEdit setTitle:#"ActionToEdit" forState:UIControlStateNormal];
[buttonToEdit addTarget:self action:#selector(editMyTextField) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonToEdit];
}
-(void)editMyTextField
{
self.textField.userInteractionEnabled = YES;
[self.textField becomeFirstResponder];
}
// The las step (to you) is in the delegate, when the editing finish, go back to no interaction
#pragma mark - TextFieldDelegate
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if ([textField isEqual:self.textField]) {
self.textField.userInteractionEnabled = NO;
// more code here, validate...
}
}
Check this: How to disable paste option in menucontroller in iOS?

How to fix offset keyboard when UISearchbar is touched

The problem I'm having is that when a user touches into the search bar the keyboard appears fine. Then when the presentQR method (see below) is called and then dismissed and then the searchbar is touched the keyboard appears as shown in the screenshot where it is offset. This is iOS7; not sure if that matters though.
I'm adding a UISearchbar using the following code:
// search bar
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(90.0, 0.0, 200.0, 44.0)];
searchBarView.autoresizingMask = 0;
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 190, 40)];
searchBar.backgroundImage = [[UIImage alloc] init];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.navigationItem.titleView = searchBar;
searchBar.delegate = self;
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,[UIColor whiteColor],UITextAttributeTextShadowColor,[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],UITextAttributeTextShadowOffset, nil] forState:UIControlStateNormal];
UITextField *textfield = [[UITextField alloc] init];
searchBar.placeholder = #"City Search";
// hide magnifying glass
textfield = nil;
for (UIView* subView in searchBar.subviews) {
if ([subView isKindOfClass:[UITextField class]]) {
textfield = (UITextField*)subView;
[(UITextField*)subView setTextColor:[UIColor whiteColor]];
[(UITextField*)subView setFont:[UIFont fontWithName:#"DIN-Regular" size:18]];
textfield.leftViewMode = UITextFieldViewModeNever;
textfield.rightViewMode = UITextFieldViewModeAlways;
break;
}
}
UIButton *cancelButton;
for (id button in searchBar.subviews)
{
if ([button isKindOfClass:[UIButton class]])
{
cancelButton=(UIButton*)button;
break;
}
}
[searchBar addSubview:textfield];
[searchBarView addSubview:searchBar];
self.navigationItem.titleView = searchBarView;
-(void)presentQR {
QRReaderViewController *qr = [[QRReaderViewController alloc]initWithNibName:#"QRReaderViewController" bundle:nil];
UIButton *removeQRBtn=[UIButton buttonWithType:UIButtonTypeCustom];
removeQRBtn.frame=CGRectMake(260, 10, 60, 40);
[removeQRBtn addTarget:self action:#selector(removeQR) forControlEvents:UIControlEventTouchDown];
[removeQRBtn setImage:[UIImage imageNamed:#"qrcode.png"] forState:0];
[qr.view addSubview:removeQRBtn];
qr.view.backgroundColor = customColorGrey;
qr.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;
// [self presentModalViewController:qr animated:YES];
[self presentViewController:qr animated:YES completion:nil];
}
-(void)removeQR {
// [self dismissModalViewControllerAnimated:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}
1st Make sure that in QRReaderViewController there is nothing like reframing window or such. There might be an element conflicting with UIWindow preventing keyboard to be shown correctly. (e.g unbalanced orientation changes.)
2nd If you didn't find anything there you could try accessing the keyboard view using the following code and attempt to reset the frame. You could put this code in keyboardWillShow method (need to register for the notification)
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html#//apple_ref/c/data/UIKeyboardWillShowNotification
PS. I would suggest to just log the keyboard frame so you know when it's changed.
Reframing Keyboard: Found here: https://stackoverflow.com/a/10957843/1017340
//The UIWindow that contains the keyboard view - It some situations it will be better to actually
//You need to 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 UIPeripheral throughout the SDK we will just use UIView.
//UIPeripheral 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];
//Assuming this is for 4.0+, In 3.0 you would use "<UIKeyboard"
if([[keyboard description] hasPrefix:#"<UIPeripheral"] == YES)
{
NSLog(#"Keyboard Frame: %#",NSStringFromCGRect(keyboard.frame));
//HERE : try reframing the keyboard
//[keyboard setFrame:CGRectMake(...)]; /*NOT TESTED NOT RECOMMENDED*/
}
}

UIScrollView with a large number of UIButtons

What I want is a UIView with lots of UIButtons in it. They get placed and arranged according to data stored in an NSArray. Because there are quite a lot of buttons they don't fit on the screen all at once. The user should be able to zoom out to see all the buttons or to zoom in to see details (the label on them) and to easily select them.
I tried two different approaches:
1) I constructed a UIView subclass, put the buttons in it and an instance of this View inside a UIScrollview.
Effect: I can access all Buttons via their tag and scrolling and zooming works fine. BUT I can't get the buttons to handle any events (press on them)...
2) I wrote a UIViewController with exactly the same functionality and added an instance of it to the UIScrollView.
Effect: I can press the buttons now, but scrolling and zooming have stopped to work.
Here the relevant Code of the View:
- (UIView *)initWithArray:(NSArray *)nArray{
self = [super init];
if (self) {
int count = [nArray count];
for (int i=0; i<count; i++) {
UIButton *button = [[UIButton alloc]
initWithFrame:(__some Code to place the Button__);
button.tag = i+1;
NSString *title = [[NSString alloc] initWithFormat:__code for generating Title__];
[button setTitle:title forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[self addSubview:button];
}
}
return self;
}
And the Code for the matrixController:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *nArray = [[NSMutableArray alloc] __some code___];
int count = [nArray count];
for (int i=0; i<count; i++) {
UIButton *button = [[UIButton alloc]
initWithFrame:CGRectMake(__some Code to place the Button__];
button.tag = i+1;
NSString *title = [[NSString alloc] initWithFormat:__code for generating Title__];
[button setTitle:title forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:button];
}
}
And the code for the ScrollViewController:
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 970)];
[self.view addSubview:scrollView];
[scrollView setBackgroundColor:[UIColor blackColor]];
//Zooming
[scrollView setMinimumZoomScale:0.25];
[scrollView setMaximumZoomScale:4.0];
[scrollView setDelegate:self];
// constructing the view
[scrollView addSubview:chartView];
[scrollView bringSubviewToFront:chartView];
OR
[scrollView addSubview:[matrixController view]];
How can I get this to work??
I'm able to get a scroll view containing multiple buttons to pan and zoom just fine, with the buttons still handling touch events:
- (void)didTapButton:(UIButton *)button
{
NSLog(#"Button %ld", (long)button.tag);
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.delegate = self;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 3.0f, scrollView.frame.size.height * 3.0f);
scrollView.maximumZoomScale = 3.0f;
UIView *zoomView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, scrollView.contentSize.width, scrollView.contentSize.height)];
zoomView.backgroundColor = [UIColor whiteColor];
for (NSInteger index = 0; index < 100; index++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake((scrollView.frame.size.width / 2.0f) - 50.0f, 10.0f + (50.0f * (CGFloat)index), 100.0f, 30.0f);
button.tag = index;
[button setTitle:[NSString stringWithFormat:#"Button %ld", ((long)index + 1)] forState:UIControlStateNormal];
[button addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
[zoomView addSubview:button];
}
[scrollView addSubview:zoomView];
[self.view addSubview:scrollView];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [scrollView.subviews objectAtIndex:0];
}
EDIT: I said not to rely on tag in my comment below, yet I'm doing in here. :) It's merely so I could log the button number, so that part should be ignored.
for (int i=0; i<10; i++)
{
UIButton *scrollingbutton_outlet = [[UIButton alloc] init];
scrollingbutton_outlet = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img = [UIImage imageNamed:#"box_normal.png"];
scrollingbutton_outlet.frame = CGRectMake(0, 100, img.size.width, img.size.height);
[scrollingbutton_outlet setTitle:[NSString stringWithFormat:#"%d",i+1] forState: UIControlStateNormal];
scrollingbutton_outlet.tag=i+1;
[buttonArray addObject:[NSString stringWithFormat:#"%d",i+1]];
buttonArray = [[NSMutableArray alloc] init];
[scrollingbutton_outlet setBackgroundImage:img forState:UIControlStateNormal];
[scrollingbutton_outlet addTarget:self
action:#selector(scrollbuttonpress:)
forControlEvents:UIControlEventTouchUpInside];
scrollingbutton_outlet.frame = CGRectMake(img.size.width*i,0, img.size.width, scrollviewoutlet.frame.size.height);
[scrollviewoutlet addSubview:scrollingbutton_outlet];
width = scrollingbutton_outlet.frame.size.width;
height = scrollingbutton_outlet.frame.size.height;
scrollviewoutlet.contentSize = CGSizeMake(width*i+scrollingbutton_outlet.bounds.size.width+5, height);
}

Resources