how to add UITextField in MKAnnotationView Title

邮差的信 提交于 2019-12-11 19:59:52

问题


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];

回答1:


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.



来源:https://stackoverflow.com/questions/19798698/how-to-add-uitextfield-in-mkannotationview-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!