如何在MKAnnotationView标题中添加UITextField

时间:2013-11-05 20:50:35

标签: iphone ios7 uitextfield mapkit mkannotation

我尝试这样,但它不起作用。我怎么能这样做?

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 个答案:

答案 0 :(得分:1)

首先,你正在为 pointText UITextField设置错误的框架,纬度和经度不是屏幕坐标,它应该像这样实现:

CGFloat originX = 0; //Or other value
CGFLoat originY = 0; //Or other value
UITextField *pointText = [[UITextField alloc] initWithFrame:CGRectMake(originX, originY, 100, 20)];

您可能 需要将canShowCallout设置为YES:

pointTest.canShowCallout = YES;

然后,您将rightCalloutAccessoryView设置为UITextField,这不是最好的选择。为此属性指定的公共视图是UIButton对象,其类型设置为UIButtonTypeDetailDisclosure。对于标题和副标题,我更喜欢使用注释属性:

point.title = "Your title";
pointTest.annotation = point;

您应该查看apple documentation