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];
Related
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.
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
In the picture below, what type of view is used to create the 'turning on reminders' activity indicator? Is it a custom view with a label and a standard UIActivityIndicator? a built in UIKit class?
As others have said, it is a custom view. Fortunately, a kind third party has created a nice open source implementation:
https://github.com/jdg/MBProgressHUD
yes it is view with a label and a standard UIActivityIndicator.
You don't need a custom view. Something like this completely untested code would do it:
- (UIView *)busyOverlayViewWithText:(NSString *)text {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(80, 120, 160, 160)];
view.opaque = NO;
view.clipsToBounds = YES;
view.backgroundColor = [UIColor colorWithWhite:0 alpha:.2];
view.layer.cornerRadius = 8;
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
spinner.center = CGPointMake(80, 30);
[spinner startAnimating];
[view addSubview:spinner];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, 160, 100)];
label.text = text;
label.textColor = UIColor.whiteColor;
label.backgroundColor = UIColor.clearColor;
label.textAlignment = UITextAlignmentCenter;
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
[view addSubview:label];
return view;
}
this is using ARC. Add (auto)releases if you use manual memory management.
This is not a single built-in class. Rather it is a composition of:
A custom transparent view, possibly an image.
A standard activity indicator.
A standard label.
If you want to create something similar then you can easily put them all in a single class.
I want to set an image and a label at the center of my UINavigationBar, along all my navigation stack.
What I'm currently doing is adding it to my navigation item titleView.
The "problem" with this approach is that I have to call this method in the viewDidLoad for each view controller I push to my navigation stack.
The other way around is to add the UILable and UIImageView directly to the UINavigationBar, however that why I have to calculate the center myself, and in addition I read that's not the recommended approach.
Any Idea how to get what I want ?
My Code:
CGRect navTitle = controller.navigationController.navigationBar.bounds;
CGFloat aHeight = navTitle.size.height;
UIView* container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 163, aHeight)];
UIImage* statusImg = [UIUtils getStatusImage];
UIImageView *statusImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,aHeight/2-statusImg.size.height/2, 33., 32.)];
statusImage.autoresizingMask = UIViewAutoresizingNone;
statusImage.image = statusImg;
statusImage.backgroundColor = [UIColor clearColor];
[statusImage setTag:1];
[statusImage setHidden:NO];
initWithFrame:CGRectMake(statusImage.frame.origin.x + 33. + 3, 0, 130., navTitle.size.height)];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(statusImage.frame.origin.x + 33. + 3, 0, 130., navTitle.size.height)];
titleLabel.textAlignment = UITextAlignmentLeft;
titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.font = [UIFont boldSystemFontOfSize:20.];
titleLabel.shadowOffset = CGSizeMake(1, -1);
titleLabel.opaque = NO;
titleLabel.backgroundColor = [UIColor clearColor];
[titleLabel setTag:2];
[container addSubview:statusImage];
[container addSubview:titleLabel];
controller.navigationItem.titleView = container;
[statusImage release];
[titleLabel release];
[container release];
Found the a nice way to do it :
Registering yourself as the delegate of UINavigationController will let you receive a callback each time a new controller is about to be pushed.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
Inside that function, getting the viewController and operating on his navigationitem will do the trick.
how to find length of the navigation bar....
i want to display a label centre of the navigation bar programmatically..
so that i can write formula ,so it should work if i rotate my device ...
Use the following code snippet in the viewDidLoad method of UIViewController.
UILabel *label = [[UILabel alloc] init];
label.text = #"Center Label";
label.frame = CGRectMake(0, 0, 100, 30);
label.textAlignment = NSTextAlignmentCenter;
UIBarButtonItem *customButton = [[UIBarButtonItem alloc] initWithCustomView:label];
self.navigationItem.titleView = customButton.customView;
Code is self explanatory. Customize the label and other necessary stuff as required.