I try like this, but it isn't work. How can i do this ?
MKAnnotationView *pointTest = [[MKAnnotationView alloc] init];
pointTest.annotation = point;
pointTest.draggable= YES;
UITextField *pointText = [[UITextField alloc] initWithFrame:CGRectMake(location.latitude, location.longitude, 100, 20)];
pointText.backgroundColor = [UIColor blackColor];
pointTest.rightCalloutAccessoryView = pointText;
pointTest.canShowCallout = YES;
[self.userMap addAnnotation:pointTest];
First of all, you are setting a wrong frame for the pointText UITextField, latitude and longitude are not screen coordinates, it should be implemented like this:
CGFloat originX = 0; //Or other value
CGFLoat originY = 0; //Or other value
UITextField *pointText = [[UITextField alloc] initWithFrame:CGRectMake(originX, originY, 100, 20)];
You may also need to set canShowCallout to YES:
pointTest.canShowCallout = YES;
Then, you are setting the rightCalloutAccessoryView as an UITextField, which is not the best thing to do. A common view to specify for this property is UIButton object whose type is set to UIButtonTypeDetailDisclosure. For titles and subtitles, I prefer using the annotation attribute:
point.title = "Your title";
pointTest.annotation = point;
You should check the apple documentation.
Related
I encounter a problem is that I need a formatted grid which contain serious of UISwitch, and my solution is to put those UISwitches on each UILabel .
However, these switches can't operate normally. Any suggestions?
Below is my code:
-(void) drawSummaryColumn : (UILabel *) myLabel :(NSString *) title
{
NSArray *sHeaders = #[NSLocalizedString(#"Tue", nil),
NSLocalizedString(#"Wed", nil),
NSLocalizedString(#"Thu", nil),
NSLocalizedString(#"Fri", nil),
NSLocalizedString(#"Sat", nil),
NSLocalizedString(#"Sun", nil),
NSLocalizedString(#"Total", nil)];
float basizSize = myLabel.frame.size.width;
CGRect r;
CGRect pos;
pos.origin.x =8;
pos.origin.y = 3;
UILabel *firstCol = [[UILabel alloc] initWithFrame:CGRectZero];
r.origin.x =0.5;
r.origin.y =0.5;
r.size = CGSizeMake(120, 40);
firstCol.text =title;
firstCol.font = [ UIFont fontWithName :#"Georgia-Bold" size : 17];
firstCol.backgroundColor = [UIColor whiteColor];
firstCol.textAlignment = NSTextAlignmentCenter;
firstCol.frame =r ;
[myLabel addSubview:firstCol ];
basizSize -= 120;
basizSize = (float) basizSize / 7;
for (NSString *label in sHeaders)
{
UISwitch *objSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
objSwitch.frame = pos;
[objSwitch addTarget:self action:#selector(switchAction:) forControlEvents:UIControlEventValueChanged];
UILabel *columns = [[UILabel alloc] initWithFrame:CGRectZero];
columns.backgroundColor = [UIColor whiteColor];
columns.textAlignment = NSTextAlignmentCenter;
[columns addSubview:objSwitch];
r.origin.x += r.size.width + 0.5f;
r.size = CGSizeMake(basizSize, 40);
columns.frame =r ;
[myLabel addSubview:columns];
}
}
try
myLabel.userInteractionEnabled = YES;
firstCol.userInteractionEnabled = YES;
columns.userInteractionEnabled = YES;
by default, a label's userInteraction property is false. Hence all it's subviews(including uiswitch) has this property set to FALSE initially. parent view (uilabel) needs to have this property ENABLED so that it's subviews also get enabled for user interaction.
I have custom MKAnnotation where i'm implementing title() method of MKAnnotation protocol. My requirement is to have callout only with button and no title. But the problem is I get callout with title and button only if I implement title() method. If string returned is nil i don't see callout bubble. Please provide me a solution.
Well, its a little hackish but it works.
set the title to be #" " (empty space) just to make the callout pop and then you can wrap your button with a view, and add the button as a subview with origin.x of whatever center is...
MKPinAnnotationView* view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"Null"];
view.canShowCallout = YES;
view.clipsToBounds = NO;
UIView * v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
v.clipsToBounds = NO;
v.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UITextField* field = [[UITextField alloc] initWithFrame:CGRectMake(-42, 0, 100, 30)];
field.textAlignment = NSTextAlignmentCenter;
field.clipsToBounds = NO;
field.autoresizingMask = UIViewAutoresizingFlexibleWidth;
field.delegate = self;
[v addSubview:field];
view.rightCalloutAccessoryView = v;
this example is with a UITextField but it will also work with your button.
Use : [YourAnotationView setCanShowCallout:NO];
I am adding a UITextField's layer as a sublayer to an existing layer that is actually a UIImageView. I apply perspective rotation on the image view and it works fine. But the UITextField does not become active. The beginFirstResponder message to it does not show the keyboard. Tapping it doesn't work, it stays inactive from user interaction. I also tried setting the zPosition for the textfield's layer so that it is above the imageview layer and it's zPosition.
rightband = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"right-band.png"]];
rightband.layer.frame = CGRectMake(623.5, 310.5, 624, 210);
rightband.layer.zPosition = 40.0f;
usernameField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 260, 26)];
usernameField.clearButtonMode = UITextFieldViewModeWhileEditing;
usernameField.keyboardType = UIKeyboardTypeDefault;
usernameField.autocapitalizationType = UITextAutocapitalizationTypeNone;
usernameField.borderStyle = UITextBorderStyleNone;
usernameField.backgroundColor = [UIColor clearColor];
usernameField.font = [UIFont fontWithName:#"HelveticaNeue" size:17];
usernameField.textColor = UIColorFromRGB(0x303030);
usernameField.textAlignment = UITextAlignmentLeft;
usernameField.placeholder = #"Username";
usernameField.autocorrectionType = UITextAutocorrectionTypeNo;
usernameField.returnKeyType = UIReturnKeyNext;
usernameField.delegate = self;
usernameField.layer.anchorPoint = CGPointMake(1.0f, 0.5f);
usernameField.layer.zPosition = 100.0f;
usernameField.layer.frame = CGRectMake(10, 10, 260, 26);
[rightband.layer addSublayer:usernameField.layer];
You should add the text field view in order for it to respond to touches.
So do :
[rightband addSubview:usernameField];
Can you try to change the view heirarchy to the following
UIView -> UIImageView and UITextField
Dont add the UITextField as a child to UIImageView
The UITextField and UIImageView will be both child's to a UIView
I have a UITextField inside of a UITableView cell. When I rotate the device, the textfield is still the same width it was for Portrait mode. I know that I need to use autoresizing to make the field fill the cell when I am in landscape, but I am not sure how to do this, as I have tried and failed.
How would I do this?
cell.textLabel.text = #"Email";
UITextField *field2 = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, width, 30)];
field2.adjustsFontSizeToFitWidth = YES;
field2.textColor = [UIColor blackColor];
field2.keyboardType = UIKeyboardTypeEmailAddress;
field2.returnKeyType = UIReturnKeyNext;
field2.placeholder = #"Required";
field2.tag = 3;
field2.delegate = self;
field2.autoresizingMask = UIViewAutoresizingFlexibleWidth;
field2.backgroundColor = [UIColor blueColor];
// Finish building cell
field2.autocorrectionType = UITextAutocorrectionTypeNo;
field2.autocapitalizationType = UITextAutocapitalizationTypeNone;
field2.textAlignment = UITextAlignmentLeft;
[field2 addTarget:self action:#selector(updateFields:) forControlEvents:UIControlEventEditingChanged];
field2.text = email;
self.emailField = field2;
[cell addSubview:field2];
[field2 release];
Check if you tableView has Auto Resize subivew is set to YES.
Check you are returning YES for all the supported rotations.
(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:
(NSTimeInterval)duration{
}
i would like to know configuration about UITextField standard please
- (IBAction)add:(id)sender
{
UITextField * textfieldToAdd = [[[UITextField alloc] init] autorelease];
// ... configuration code for textfield ...
[self.view addSubview:textfieldToAdd];
}
Here is a code from Apple's example UICatalog
#pragma mark -
#pragma mark Text Fields
- (UITextField *)textFieldNormal
{
if (textFieldNormal == nil)
{
CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);
textFieldNormal = [[UITextField alloc] initWithFrame:frame];
textFieldNormal.borderStyle = UITextBorderStyleBezel;
textFieldNormal.textColor = [UIColor blackColor];
textFieldNormal.font = [UIFont systemFontOfSize:17.0];
textFieldNormal.placeholder = #"<enter text>";
textFieldNormal.backgroundColor = [UIColor whiteColor];
textFieldNormal.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textFieldNormal.keyboardType = UIKeyboardTypeDefault; // use the default type input method (entire keyboard)
textFieldNormal.returnKeyType = UIReturnKeyDone;
textFieldNormal.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textFieldNormal.tag = kViewTag; // tag this control so we can remove it later for recycled cells
textFieldNormal.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
// Add an accessibility label that describes what the text field is for.
[textFieldNormal setAccessibilityLabel:NSLocalizedString(#"NormalTextField", #"")];
}
return textFieldNormal;
}
- (UITextField *)textFieldRounded
{
if (textFieldRounded == nil)
{
CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);
textFieldRounded = [[UITextField alloc] initWithFrame:frame];
textFieldRounded.borderStyle = UITextBorderStyleRoundedRect;
textFieldRounded.textColor = [UIColor blackColor];
textFieldRounded.font = [UIFont systemFontOfSize:17.0];
textFieldRounded.placeholder = #"<enter text>";
textFieldRounded.backgroundColor = [UIColor whiteColor];
textFieldRounded.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textFieldRounded.keyboardType = UIKeyboardTypeDefault;
textFieldRounded.returnKeyType = UIReturnKeyDone;
textFieldRounded.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textFieldRounded.tag = kViewTag; // tag this control so we can remove it later for recycled cells
textFieldRounded.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
// Add an accessibility label that describes what the text field is for.
[textFieldRounded setAccessibilityLabel:NSLocalizedString(#"RoundedTextField", #"")];
}
return textFieldRounded;
}
- (UITextField *)textFieldSecure
{
if (textFieldSecure == nil)
{
CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);
textFieldSecure = [[UITextField alloc] initWithFrame:frame];
textFieldSecure.borderStyle = UITextBorderStyleBezel;
textFieldSecure.textColor = [UIColor blackColor];
textFieldSecure.font = [UIFont systemFontOfSize:17.0];
textFieldSecure.placeholder = #"<enter password>";
textFieldSecure.backgroundColor = [UIColor whiteColor];
textFieldSecure.keyboardType = UIKeyboardTypeDefault;
textFieldSecure.returnKeyType = UIReturnKeyDone;
textFieldSecure.secureTextEntry = YES; // make the text entry secure (bullets)
textFieldSecure.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textFieldSecure.tag = kViewTag; // tag this control so we can remove it later for recycled cells
textFieldSecure.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
// Add an accessibility label that describes what the text field is for.
[textFieldSecure setAccessibilityLabel:NSLocalizedString(#"SecureTextField", #"")];
}
return textFieldSecure;
}
- (UITextField *)textFieldLeftView
{
if (textFieldLeftView == nil)
{
CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);
textFieldLeftView = [[UITextField alloc] initWithFrame:frame];
textFieldLeftView.borderStyle = UITextBorderStyleBezel;
textFieldLeftView.textColor = [UIColor blackColor];
textFieldLeftView.font = [UIFont systemFontOfSize:17.0];
textFieldLeftView.placeholder = #"<enter text>";
textFieldLeftView.backgroundColor = [UIColor whiteColor];
textFieldLeftView.keyboardType = UIKeyboardTypeDefault;
textFieldLeftView.returnKeyType = UIReturnKeyDone;
textFieldLeftView.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textFieldLeftView.tag = kViewTag; // tag this control so we can remove it later for recycled cells
// Add an accessibility label that describes the text field.
[textFieldLeftView setAccessibilityLabel:NSLocalizedString(#"CheckMarkIcon", #"")];
textFieldLeftView.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"segment_check.png"]];
textFieldLeftView.leftViewMode = UITextFieldViewModeAlways;
textFieldLeftView.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
}
return textFieldLeftView;
}
Go through the reference doc for UITextField Class also try to read UITextFieldDelegate. Look for the different tasks that can be performed and see the methods, properties available for it. And use it according to your requirement.