I want to create popup in iOS8 custom keyboard as shown below image.
Some code are working but can't access outer window of keyboard and occures issue as shown in below image-2
This what i have done in my custom Keyboard its working
//adding pop up when character is tapped
- (void)addPopupToButton:(UIButton *)button
{
CGRect frame,frame1;
if(self.view.frame.size.width == 320)
{
//Keyboard is in Portrait
frame = CGRectMake(0, -25, 28, 43);
frame1=CGRectMake(0, 0, 28, 43);
}
else{
//Keyboard is in Landscape
frame = CGRectMake(3, -25, 35, 43);
frame1=CGRectMake(0, 10, 35, 43);
}
//create pop up view
UIView *popUp=[[UIView alloc]initWithFrame:frame];
//create a label to add to pop up view
UILabel *text = [[UILabel alloc] init];
//set frame for the label and set label title
[text setFrame:frame1];
[text setText:button.titleLabel.text];
text.textAlignment=NSTextAlignmentCenter;
[text setFont:[UIFont boldSystemFontOfSize:30]];
text.backgroundColor=[UIColor whiteColor];
//add label as popup view's subview
[popUp addSubview:text];
//add pop up view as button's subview
[button addSubview:popUp];
}
//remove Pop up view
-(void)endPopUpForButton:(UIButton*)button
{
if ([button subviews].count > 1)
{
[[[button subviews] objectAtIndex:1] removeFromSuperview];
}
}
From the Apple App Extension Programming guide:
Finally, it is not possible to display key artwork above the top edge
of a custom keyboard’s primary view, as the system keyboard does on
iPhone when you tap and hold a key in the top row.
So it seems like you can't add pop-up outside the keyboard frame, so codelgnitor's answer is the best you can do.
Related
I am seeing a strange behavior of UILabel and its subview on iOS development.
I want to have a overlay view on the UILabel and want to change only the text of the label.
It works well when the text is digits or alphabets.
However when I changed the text to a Unicode string, the subview disappears.
To reproduce the problem, I created a simple project which just have a label and a button.
The label has a "A" as the initial text.
When the button was tapped, the following method will be called and change the text of UILabel cyclically.
- (void)pushButton:(id)sender
{
if ([[_label text] isEqualToString:#"A"]) {
[_label setText:#"B"];
// add a subview on the label
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
[view setBackgroundColor:[UIColor blueColor]];
[_label addSubview:view];
} else if ([[_label text] isEqualToString:#"B"]) {
[_label setText:#"C"];
} else if ([[_label text] isEqualToString:#"C"]) {
[_label setText:#"D"];
} else if ([[_label text] isEqualToString:#"D"]) {
[_label setText:#"\u2605"];
// after this, the subview disappears
} else {
[_label setText:#"A"];
// after this, the subview appears again
}
}
When I tap the button at the first time, the text is changed to "B" and the subview appears.
Second time and third time, the text is changed to "C" and "D" and the subview is still there.
Howerver the fourth time, the text is changed to "★" and the subview disappears.
Fifth time, the text is changed to "A" and the subview appears again.
In this code, I don't remove the subview and the new subview is created in every cycle.
However in any time the text was changed to "★", all these subviews disappear.
How can I keep the subview being appeared on the UILabel?
UILabel is not intended to have subviews. The solution is to make your blue rectangle a subview of your label's superview. You can position it so that it appears in front of your UILabel.
Thus, where you have this code:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
[view setBackgroundColor:[UIColor blueColor]];
[_label addSubview:view];
You would instead say this:
UIView *sup = [_label superview];
UIView *view = [UIView new];
[view setBackgroundColor:[UIColor blueColor]];
CGRect r = CGRectMake(0,0,20,20);
r = [sup convertRect:r fromView:_label];
view.frame = r;
[sup addSubview: view];
I have a text view in which i want a button for camera. In my application i want that user type some text then he/she want to attach a photo or video. I have done that but the camera button i want inside the text view. when i try to add a button as subview that button scrolled with the text but i want button fixed and another problem is that the text comes in button area not visible so i want that the cursor will not go in that area which is covered by button.
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(0, 0, 50.0, 30.0);
[editButton addTarget:self action:#selector(editPressed:)forControlEvents:UIControlEventTouchUpInside];
myButton.backgroundColor=[UIColor blueColor];
[txtView addSubview:myButton];
-
Try this .It will definitely work for you and will solve your issue
Write below code in ViewDidLoad Method.
.
-(void)textResizeForButton{
CGRect buttonFrame = self.button.frame;
UIBezierPath *exclusivePath = [UIBezierPath bezierPathWithRect:buttonFrame];
self.textView.textContainer.exclusionPaths = #[exclusivePath];
}
Add it on self.view not on UITextField so it won't be scrolled anywhere. Just get the CGrect right.
For offsetting the text on the right to fit UIButton try this:
UIView *rightMarginView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, leftMargin, txtField.frame.size.height)];
rightMarginView.userInteractionEnabled = NO;
[txtField setRightView:rightMarginView];
txtField.rightViewMode = UITextFieldViewModeAlways;
Hi in my application have i have popup view . I have made transparent background and I'm using UIView on top of it but in simulator its showing only half in the screen. like below images.
This is in my UIviewcontroller image
In simulator its showing like this.
Try changing your simulator to 3.5 Inch, because from what I see you haven't add autolayout to your Views to support 4 Inch.
Learn more about Autolayout
Most probably the registration controls (UILabel and UItextviews) are added on self.view instead on the view that is of half screen size.
You could go for the code below:
-(void)viewDidLoad
{
backgroundButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)]; //Background button to handle tap outside popup view
[backgroundButton addTarget:self action:#selector(tapDetected) forControlEvents:UIControlEventTouchUpInside];
}
-(void)tapDetected /* When user tap outside popup view so as to dismiss the view*/
{
[backgroundButton removeFromSuperview];
[tempView removeFromSuperview];
}
-(void)buttonClicked:(UIButton *)sender /*To create and open popup view */
{
[self.view addSubview:backgroundButton];
tempView = [[UIView alloc] initWithFrame:CGRectMake(15, 100, 300, 300)]; //PopUp View
[tempView setBackgroundColor:[UIColor redColor]];
[backgroundButton addSubview:tempView];
[tempView setAlpha:1];
}
I really need your help with this one (first post on SO -- be gentle):
I have two dynamic UIButtons which I would like to have centered in a UIView, which in turn should be centered in a UINavigationbar and UIToolbar. I can't - despite a lot of Googling - seem to figure out a proper way to do this.
This is what I've done so far:
In viewDidLoad, I add the two buttons as subviews and set the view as the UINavigationbar's titleView
self.myClass.viewForTitleAndButton = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 32)];
[self.myClass.viewForTitleAndButton setBackgroundColor:[UIColor blackColor]];
[self.myClass.viewForTitleAndButton addSubview:self.myClass.myButton];
[self.myClass.viewForTitleAndButton addSubview:self.myClass.myOtherButton];
self.navigationItem.titleView = self.myClass.viewForTitleAndButton;
In a method being triggered when I press certain buttons, I set the title (and bounds) of one of the buttons depending on what's clicked:
CGSize titleSize = [title sizeWithAttributes:#{NSFontAttributeName : [UIFont italicSystemFontOfSize:17.0]}];
CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGFloat newX = (screenSize.width - titleSize.width) / 2;
CGRect buttonFrame = self.myClass.myButton.frame;
//Removing the line below doesn't do any difference at the moment
self.myClass.myButton.bounds = CGRectMake(newX, buttonFrame.origin.y, titleSize.width+8, buttonFrame.size.height);
[self.myClass.myButton setTitle:title forState:UIControlStateNormal];
NSLog(#"Title: %#", title);
//title is a NSString that changes depending on what is clicked. I am 100% sure it changes as I can see it in the log every time the method is triggered.
The problem is that the title of myButton is not changed. It worked before with the very same button when it was placed in a different spot and not as a subview.
Q1: What am I missing to make the title of the button change?
Q2: Is adding the buttons as subViews to a view that is then placed in the navigationbar and toolbar respectively a sound way to accomplish what I want?
This is really bugging me, any pointers in the right direction is much appreciated.
I don't think you can add a button which is already an outlet of a view controller. Thinking in another way, a button(view) can only has one superview.
Create button dynamically for the title view.
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *iv = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 160, 44)];
iv.backgroundColor = [UIColor greenColor];
titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
titleButton.frame = CGRectMake(0, 0, 120, 44);
[titleButton setTitle:#"test" forState:UIControlStateNormal];
[titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[iv addSubview:titleButton];
self.navigationItem.titleView = iv;
}
//some event to change the button title
- (void)click:(UIButton *)button
{
[titleButton setTitle:#"I changed" forState:UIControlStateNormal];
}
I'm trying to create a horizontally scrollable menu similar to that in this video.
For some reason the UIView doesn't appear after adding a bunch of UIButtons to it and adding it to the UIScrollView. Here's my code (it's called in -viewDidLoad of a subclass of UIViewController):
//set up scrollview
UIScrollView *designPicker = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 431, 320, 49)];
//set up a view to drop into the scroll view
UIView * buttonsView = [[UIView alloc] initWithFrame:CGRectMake(0, 431, 640, 49)];
//add buttons to scrollview
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
float runningX = designPicker.frame.origin.x;
for (i = 1; i <= 10; i++)
{
UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[tempBtn setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
CGRect rect = CGRectMake(runningX, designPicker.frame.origin.y, 30.0, 30.0);
tempBtn.frame = rect;
[buttonsView addSubview:tempBtn];
runningX = runningX + 35;
[tempBtn release];
}
[designPicker setContentSize:buttonsView.frame.size];
[designPicker addSubview:buttonsView];
[self.view addSubview:designPicker];
You should not add the buttons using the frame of the UIScrollView. The origin of the frame is in the superview's (superview of the UIScrollView) coordinates. You should make the buttons' frame relative to the UIView. So if you want the buttons to appear at the top of the view that you should start at (0,0).
Instead of adding scrollview as subview to your view, add view as subview of scrollview. As-
[self.scrollView addSubview:self.view];
// release scrollView as self.view retains it
self.view=self.scrollView;
[self.scrollView release];
And make sure your view should have large content size than content size of your scrollview.It worked for me.