How put a UIView above the keyboard - objective-c

I have a idea to hide some keys in keyboard. I use the exact background of the keyboard and use it above the keyboard.
So I create a textView and add a accessoryView
//Create the view
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -10, 768, 300)];
[view setBackgroundColor:[UIColor greenColor]]
UITextField text = [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 80, 28)];
[text setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
[text setInputAccessoryView:view];
This really hide the keyboard, but if I click in this view, the button still show. I try to use a UIButton, but the button don't catch the click. And I try becameFirstResponder when UIKeyboardDidShowNotification and no success.
Any Ideas?

You can set userInteractionEnabled to YES for your accessory view. This will make your view swallow all the touches.
If you want a custom view for editing, though, you can do this by setting a specific editing view for your field rather than trying to put something else on top of the keyboard.

Related

Elements position get changed after adding them as subView

I put a UITextField, a UITextView and another element to a UIView and set their position in Utility Area in Xcode. What I want to do is group them as a subView and apply some animations to the subView.
But after adding them to the newly created subView change their position, look at this pictures (before and after adding them):
Before
After
The green UITextField is center aligned and the UITextView is disappeared! (I don't care about the UILabel and that horizontal line right now).
It's my code:
CGRect oldPosition = taskTitle.frame;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
[view setBackgroundColor:[UIColor redColor]];
[view addSubview:taskTitle];
[view addSubview:taskNote];
[view addSubview:datePickerButton];
taskTitle.frame=oldPosition;
[self.view addSubview:view];
Disable autolayout from the file inspector, in Utilities bar.
It can be done by unchecking the Use Autolayout option.
Here is a screenshot:

UIButton not showing up

I am trying to create a UIButton programmatically instead of using the interface builder. I initialize and set the button frame but the button doesn't seem to appear on the view. My code is as follows:
UIButton *showInfoButton = [[UIButton alloc] init];
CGRect buttonFrame = CGRectMake(0,0, 150, 150);
showInfoButton.frame = buttonFrame;
What am I missing? Please bear with me as I am new to iOS.
You forgot to add your UIButton as a subview to the UIView you want it to display on. Since you are building UI programmatically, you need to manually add the button as a subview to the container view.
You can do so by using this line of code:
[self.view addSubview:showInfoButton];
The button might not still appear as you are making a custom button which has a transparent background colour. In order to see the button, you can set the UIButton's backgroundColor property as follows:
[showInfoButton setBackgroundColor:[UIColor blackColor]];

UITableViewCell Delete button screws up textLabel when using line break in text

I have a UITableViewCell, when swipe to bring up the Delete button it screws up the textLabel because I am using a line break in text.
Before:
In delete mode:
This is frustrating because I just want the delete button to go over my text, rather than trying to animate it out of the way which screws it up.
Why does this happen? Because I use a line break in my string?
How can I prevent this from happening?
Why does this happen? Because I use a line break in my string?
It happends because you've added the label to the contentView of the cell (which is good). When the delete button appears the contentView will be automatically resized and the UITableViewCell's textLabel will resize as well because it's autoresizingMask is set to UIViewAutoresizingFlexibleWidth. Unfortunately we cannot override the autoresizingMask of the default textLabel.
How can I prevent this from happening?
You'll have to build a custom cell yourself. Subclass UITableViewCell with a custom UILabel as a property and set it's autoresizingMask to UIViewAutoresizingNone.
//create and set your frame to whatever you want.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 110, 20)];
//set autoresizing
[label setAutoresizingMask:UIViewAutoresizingNone];
//add to view
[[self contentView] addSubview:label];
//set property to acces later
[self myLabel:label];
//release (if you aren't using ARC)
[myLabel release];
add some sample text to test it later on
[[cell myLabel] setText:#"MAX: 72.92 MPH Will fit!"];
If you're using interfacebuilder to create your view/cells make sure the autoresizing mask arrow's are grayed out.
You should be able to access the UITableViewCell's textLabel.Frame property. Subclass UITableViewCell and set this in [UITableViewCell didTransitionToState:(UITableViewCellStateMask)state]

customize UIBarButtonItem to have no border?

I'm trying to create a UIBarButtonItem that looks like the "list" button on the iPod.app
so I have 2 questions:
How can I set the icon to look like the iPod's one? I saw it in other apps, but couldn't find it on xcode. (the icon that appears on the right side of the navigation bar on the "now playing" screen)
how can I set the UIBarButtonItem's image to show without it's default border?
Try adding custom view with button you want as a subview to your bar;
UIView* myView = [[UIView alloc] initWithFrame:myBar.bounds];
UIbutton* myBtn = [[UIButton alloc] initWithFrame:CGRectMake(x,y,width,height)];
[myBtn setImage:myImage forState:UIControlStateNormal];
[myView addSubview:myBtn];
[myBar addSubview:myView];

UINavigationBar with customized background - how to make rightBarButton visible

I've customized background of navigation bar in my RootViewController (just part of code)
[navBar insertSubview:customBack atIndex:0];
I push detailViewController and add activity indicator as a rightBarButtonItem
UIActivityIndicatorView *actInd = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
self.activityIndicator = actInd;
[actInd release];
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:self.activityIndicator];
[self.navigationItem setRightBarButtonItem:barButton];
[barButton release];
The problem is that the indicator is not visible but without customized background it works OK.
You can better change your navigationBar Background I think. Create a subclass of UINavigationBar and add:
- (void)drawRect:(CGRect)rect {
UIImage * image = [UIImage imageNamed:#"MyNavigationBarBackground.png"];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextScaleCTM(ctx, 1.0, -1.0); // Otherwise the image is drawn upside-down
CGContextDrawTiledImage(ctx, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
}
I do that and I never add a problem with any button :)
Zoleas has the right idea. If you don't or can't use a subclass, I think your problem is that you are adding your subview at index 0 so it is on top of the other views in the navigation bar, it is probably hiding your buttons.
set rightBarButtonItem with following way
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Exit" style:UIBarButtonItemStylePlain target:self action:#selector(cancel)];
Try this and also check the position of background
The activity indicator is there its just not being activated. Try animating it like this:
[actInd startAnimating];
Or if you want to keep it visible set the hideWhenStopped property like this:
actInd.hidesWhenStopped = NO;
Did you try to change the origin from the UIActivityIndicatorView?
Or creating it inside an UIView and add this UIView to the UIBarButtonItem.
I think the problem can be the frame from the element.
By this way you can resolve the problem:
Create activity indicator and add in a view1
Now this view1 add or create navigation bar with the help of this view1. Now when view1 is visible automatically you will be able to see the loading indicator.
Just wild try on this.