我创建了一个在MapView注释标注中显示的视图。
如果我没有使用xib并使用initWithFrame
实例化此视图,则没有问题。
但是如果我使用xib,地图上显示的尺寸应该比应该更大。
视野中的代码:
@interface CalloutContentView : UIView
@property (nonatomic) Visit *visit;
+ (id)customView;
@end
#import "CalloutContentView.h"
@interface CalloutContentView ()
@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UILabel *subtitle;
@property (weak, nonatomic) IBOutlet UISegmentedControl *sc;
@end
@implementation CalloutContentView
+ (id)customView
{
CalloutContentView *customView = [[[NSBundle mainBundle] loadNibNamed:@"CalloutContentView" owner:self options:nil] lastObject];
if ([customView isKindOfClass:[CalloutContentView class]])
return customView;
else
return nil;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// _title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, w, 18)];
_title.font = [UIFont boldSystemFontOfSize:15.0];
// _title.textAlignment = NSTextAlignmentLeft;
_title.textColor = [UIColor colorWithWhite:0.199 alpha:1.000];
_title.numberOfLines = 1;
_title.backgroundColor = [UIColor yellowColor];
[self addSubview:_title];
// _subtitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 25.0, w, 14)];
_subtitle.font = [UIFont systemFontOfSize:12.0];
_subtitle.textAlignment = NSTextAlignmentLeft;
_subtitle.textColor = [UIColor colorWithWhite:0.239 alpha:1.000];
_subtitle.backgroundColor = [UIColor clearColor];
[self addSubview:_subtitle];
// _sc = [[UISegmentedControl alloc]initWithItems:@[@"One",@"Two"]];
_sc.frame = CGRectMake(0, self.frame.size.height - 30, w, 30);
[_sc addTarget:self action:@selector(segmentedControlValueDidChange:) forControlEvents:UIControlEventValueChanged];
[self addSubview:_sc];
}
return self;
}
@ end
在MapViewController中:
- (void)popupMapCalloutView {
// Change this for creating your Callout View
CalloutContentView *customView = [CalloutContentView customView];
// CalloutContentView *customView = [[CalloutContentView alloc]initWithFrame: CGRectMake(0, 0, 10, 90)];
// if you provide a custom view for the callout content, the title and subtitle will not be displayed
_calloutView.contentView = customView;
VisitAnnotationView *bottomPin = _visitAnnotationViews[_idAnn];
bottomPin.calloutView = _calloutView;
[_calloutView presentCalloutFromRect:bottomPin.bounds
inView:bottomPin
constrainedToView:_mapView
permittedArrowDirections:SMCalloutArrowDirectionAny
animated:YES];
}
使用xib时的样子如下: