UIView stuck behind UITableview header - ios

In my app I have a UITableViewCell with a UITextField in it. When the user starts typing in this textfield, an autocomplete view appears.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (self.suggestions != nil) {
self.autocomplete = [[AutoComplete alloc] initWithFrame:CGRectMake(10, 53, self.frame.size.width-20, 200)];
self.autocomplete.delegate = self;
self.autocomplete.suggestions = self.suggestions;
[self addSubview:self.autocomplete];
[self bringSubviewToFront:self.autocomplete];
}
return YES;
}
This is nothing more than a UIView with a UITableView inside it. However, what happens is that this view is hidden behind the section header and the next cell.
So while it appears to be inserted above the cell, it registers the tapp below it. When you click in the autocomplete it registers the click in the next cell. How can I fix this?

Your issue is you are adding it to the view and not the tableview which resides in the UIView as well as having a section header that will probably block it out as well. I see your y position is only 53, so your header is likely blocking it out.
I am not 100% sure where you want your result to be viewed and used, that's not clear in your question. If you want your view to be above everything else you could:
"I have a UITableViewCell with a UITextField in it. When the user starts typing in this textfield, an autocomplete view appears." - add the result view to your cell, not the main view; [cell.contentView addSubview...
Shift your tableview down by changing it's y position to self.autocomplete.view.frame.size.height. Add a nice little animation to it as it changes position too, always something that bit extra.
Consider adding it as a subview to your TableView and bringing to front, should work.
(little bit extra on the cell.contentView)

Related

UITableView as subview of UITextView not properly displayed

I'm trying to implement an autocomplete UITextView. The auto-suggestion is working fine. But the UITableView is getting clipped off. Please look at the image below.
The greybox is the actual UITableView. This UITableView is defined in another .xib file and is being called from another ViewController.
autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, 320, 35) style:UITableViewStylePlain];
autocompleteTableView.delegate = self;
autocompleteTableView.dataSource = self;
autocompleteTableView.scrollEnabled = YES;
[self.textViewCell addSubview:autocompleteTableView];
here autocompleteTableView is the UITableView and textViewCell is the UITextView. And this is getting called from another ViewController which makes the autocomplete box to constrict to UITextView size.
What i want to achieve is something like this :
Your problem would appear to be because of the view you are adding the table view as a subview of. Even if the table view was visible it most likely wouldn't respond to touches because of the frame of the superview (taps outside the frame aren't handled).
You should consider doing something like adding a new view higher up the view hierarchy which replaces (overlays) the text field and adds the table view. This needs to be added high enough up the hierarchy that the host view is big enough to fully contain the new view (so, the view controllers view).
It should work if you handle the text field as you currently are but add just the table view at the top level of the view hierarchy.

UIView inside an UIScrollView did not refresh itself despite call of method "setNeedDisplay"

I've got an UIScrollView with inside, some views loaded from a xib file.
The UIScrollView loads only three Views. The current, the left one and the right one.
For exemple, I have one view at the left and one view at the right of the current View. If I scroll to the right, the UIScrollView will delete the view to the left, scroll to the right to the new current View and load the new view to the right of the new current View.
In addition, I have a button outside the UIScrollView. When I click on it, it changes the background color of the current view displayed on the UIScrollView.
It works well but sometimes, I don't know why, when I click on the button to change the background color of the view is well changed, but the view is not refresh so the user can't see the change of background color.
The UIScrollView:
container = [[UIScrollView alloc] initWithFrame:frame];
[container setBackgroundColor:[UIColor clearColor]];
[container setCanCancelContentTouches:NO];
[container setClipsToBounds:NO];
[container setShowsHorizontalScrollIndicator:NO];
[container setPagingEnabled:YES];
The method call when I click on the button to change the background color of the current view
- (void)menuColor:(MenuPickerViewController *)controller didPickOption:(UIView *)button
{
// Get the object containing the data of the product linked with the view.
MyProduct *product = (MyProduct *)[MyProduct getProduct:[_slider getCurrentContentDisplayed]];
// Get the the superview of the button sender to have an access for the attributes of this button (color selected, ...)
ColorButtonMenu *colorView = (ColorButtonMenu *)[button superview];
// Get the current UIView displayed in the UIScrollView
MyView *myView = (MyView *)[self sliderGetViewWithID:[_slider getCurrentContentDisplayed] FromSlider:_slider];
// I check with debugger, the color is well setted
product.color = colorView.color;
// "border" is a view in my xib that I want change its background color.
IFPrint(#"myView.border backgorund color before: %#\n", myView.border.backgroundColor.description);
[myView.border setBackgroundColor:[Utilities colorFromHexString:colorView.color]];
[myView.border setNeedsDisplay];
IFPrint(#"myView.border backgorund color after: %#\n", myView.border.backgroundColor.description);
IFPrint(#"=== DEBUG ===\n");
IFPrint(#"isMainThread ? : %i\n", [NSThread isMainThread]); // Always return YES
IFPrint(#"myView: %#\n", myView); // Always return the address of a valid pointer
IFPrint(#"myView border: %#\n", myView.border); // Always return the address of a valid pointer
IFPrint(#"=============\n\n");
}
So, as you can see at the end of the method, I tried to call method setNeedsDisplay on the view loaded from a xib and the other view inside "border" but nothings work.
Moreover, my method is always called on the main thread.
Any suppositions ?
Thanks !
Edit:
Obviously, I have tested if the view return by sliderGetViewWithID is the correct view. All the attributes are well set. In my opinion it's truly a refresh problem.
are you sure the view you're trying to set background color for is actually visible? I guess it might be offscreen so you see nothing happening.
You might want to trap that event by finding the intersection between scrollview's bounds and myView's frame. if there's no intersection, that means myView is not actually visible.
so the code is:
BOOL intersects = CGRectIntersectsRect(scrollview.bounds, myView.frame);
if(intersects == NO)
{
NSLog(#"myView is offscreen");
}
Problem solved :
After setting the background color of the view. I remove the view from the UIScrollView and I re add it inside the UIScrollView. It's a bit tricky but it works good !
[myView removeFromSuperView];
[myScrollView addSubview:myView];

Make UITextView parent be its own inputAccessoryView

I'm trying to achieve a similar keyboard interaction that Messages has in iOS 7. I have a UIView which contains a UITextView, and when the user selects it to start typing, I want to make this UIView the inputAccessoryView. This would take care of the animation for me, as well as the new UIScrollView keyboard dismiss interaction in iOS 7.
When the UITextView begins editing, I'm trying to set its inputAccessoryView to its parent UIView (which is already in the view hierarchy). The keyboard appears but not with an accessory view.
I've read some people are using a duo of UITextFields to make this work, but that seems like a bad way to achieve this.
Any suggestions?
A much easier solution is to make your input field the input accessory view of your view controller:
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (UIView *)inputAccessoryView
{
return self.yourInputField;
}
The view will be on screen at the bottom of the screen and when it becomes first responder in response to a user tapping it, the keyboard will be presented. The view will be animated such that it remains immediately above the keyboard.
The only way to get this to work is via a second text field. The idea is to make it a subview but not visible (due to crazy rect). You then switch firstResponder back and forth between it and the real text field while its getting delegate methods. I created a some one viewController test project and did this (you can copy paste and verify behavior with about 2 minutes of time):
#implementation ViewController
{
UITextField *field;
UITextField *dummyView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
field = [[UITextField alloc] initWithFrame:CGRectMake(0, 460, 320, 20)];
field.borderStyle = UITextBorderStyleRoundedRect;
field.delegate = self;
//field.inputAccessoryView = field;
field.text = #"FOO";
[self.view addSubview:field];
dummyView = [[UITextField alloc] initWithFrame:CGRectMake(0, 40000, 320, 20)];
dummyView.delegate = self;
[self.view addSubview:dummyView];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == field && textField.superview == self.view) {
[field removeFromSuperview];
dummyView.inputAccessoryView = field;
[dummyView becomeFirstResponder];
}
return YES;
}
#end
I should add I've used this technique in shipping apps since iOS 4.
EDIT: So a couple of other ideas:
1) To make the glitch when the keyboard starts moving look a little better, you could take a snapshot of your textView, put that into a UIImageView, and when you remove the textView from the primary view, replace it with the UIImageView. Now the appearance is the same. Add an animation for the image so that noting happens for 50 ms, then the alpha goes to 0. Add a similar animation to your real textview, so that it has an alpha of 0 for 50 ms, then it goes to 1. You may be able to tweak this so the transition is good (but not great).
2) The way apple probably does this is to get the animation curve and timing from the keyboard moving notification. In this case they would add a accessory view with 0 height at first, and animate the textField so its tracking the keyboard, but above it. Both moving same distance at the same time. At the end of the animation, the textField is pulled out of self.view, the accessory view has its frame changed to have the height of the textField, and the textField is placed as a subview of the accessory container view. This should work but yeah, its a bit complex to do. If you want someone to code it for you offer a 100 pt bounty. You still need the dummy text field for when you go and move the textField at the end, since when you take it out of its containing view it will resign first responder. So at the end, you make the dummy field the first responder, move the textfield, then make the real textfield the first responder again.
This actually works best if you don't use .inputAccessoryView at all and instead just animate the position of the parent UIView as the keyboard opens and closes. Here is an answer describing the process step-by-step with all the code.

UITableView delete button overlaps to content

I'm preparing a UITableView with a custom prototype cell having a UISwitch widget on the right side, and I'd like to let my users be able to delete rows.
Everything is fine with that, except the fact that when the delete button shows up it overlaps to the UISwitch, this way:
Is it possible to have the UISwitch shifting left when the delete button appears?
Epilogue
I've decided for brevity to not shift my UISwitch position when "delete" button appears, but to make it disappear, bringing it back when the "delete" button is gone.
So, according to #geo suggestion (thank you), I've managed it out (quite simply) this way:
In my UITableViewCell' subclass .m file:
- (void)willTransitionToState:(UITableViewCellStateMask)state
{
[super willTransitionToState:state];
if (state & UITableViewCellStateShowingDeleteConfirmationMask) {
activationSwitch.hidden = YES;
}
else {
activationSwitch.hidden = NO;
}
}
I hit a very similar problem and fixed it. You need the right autoresizing mask (assuming not doing auto layout here) for your UI elements in your custom tableview cell in Interface Builder.
In my case, I needed to add the Left constraint (see that little autoresizing picture/animation in the Size Inspector, View section) for each of my UI elements.
Add a Left "bar", and you should be good.
Override the Methode
-(void)willTransitionToState:(UITableViewCellStateMask)state
-> UITableViewCellStateShowingDeleteConfirmationMask
of your custom Cell and do there your customizing :)

Bringing a subview in a UITableViewCell to front

I added a bunch of UILabel views to a UITableViewCell's contentView, some of which may overlap. On tapping any of the labels, I want to trigger some actions. Also, I want to bring up the tapped label to the top.
Using a UITapGestureRecognizer on the labels, I can figure out which one is tapped and perform the actions. But bringing the tapped and overlapped label to the front does not work. This is what I am trying:
UILabel *foundLabel = ....; // find the label
for (UITableViewCell *acell in theTable.visibleCells) {
UIView *cellContentView = acell.contentView;
if ([cellContentView.subviews containsObject:foundLabel]) {
[cellContentView bringSubviewToFront:foundLabel];
NSLog(#"should bring to front...");
}
}
I do get the NSLog output above, so I know that the bringSubviewToFront is being called on the appropriate cell's contentView. But no change in the subview layout order.
Ideas?
One thing to explore is zposition on the uitablviewcell's layer.
This successfully put on tableviewcell in front of another for me:
cell1.layer.zPosition=3 ;//above
cell2.layer.zPosition=2 ;//below
bringSubviewToFront: didn't work for me either
Try insertSubview:belowSubview: where the latter subview is a super view such as tab bar, nav bar, table view.

Resources