🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
IPhone中的使用时注意雷区: 1. 必须实现以下代理,否则无法使用: ``` #pragma mark - <UIPopoverPresentationControllerDelegate> - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller { return UIModalPresentationNone; } ``` 2. 必须使用`UIViewController`的`子类`中使用,简单使用例子如下: ``` TestViewController *testVC = [[TestViewController alloc] init]; testVC.preferredContentSize = CGSizeMake(150, 150); testVC.modalPresentationStyle = UIModalPresentationPopover; testVC.popoverPresentationController.delegate = self; testVC.popoverPresentationController.sourceView = _button; testVC.popoverPresentationController.sourceRect = CGRectMake(0, 0, _button.bounds.size.width / 2.0, _button.bounds.size.height / 2.0); testVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionLeft; testVC.popoverPresentationController.backgroundColor = [UIColor redColor]; testVC.popoverPresentationController.canOverlapSourceViewRect = NO; [self presentViewController:testVC animated:YES completion:^{ }]; ``` 3. 调整建议,如需去掉背景蒙版,可通过重写`UIPopoverBackgroundView`,如下: ``` @interface LYIMOrderTipBackgroundView : UIPopoverBackgroundView @end @implementation LYIMOrderTipBackgroundView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.arrowDirection = UIMenuControllerArrowUp; [self setHidden:YES]; } return self; } //是否使用默认的内置阴影和圆角 + (BOOL)wantsDefaultContentAppearance { return NO; } - (void)layoutSubviews { [super layoutSubviews]; self.backgroundColor = [UIColor clearColor]; } //这个方法返回箭头宽度 + (CGFloat)arrowBase { return 0; } //这个方法中返回内容视图的偏移 + (UIEdgeInsets)contentViewInsets { return UIEdgeInsetsMake(0, 0, 0, 0); } //这个方法返回箭头高度 + (CGFloat)arrowHeight { return 0; } //这个方法返回箭头的方向 - (UIPopoverArrowDirection)arrowDirection { return UIPopoverArrowDirectionUp; } //这个在设置箭头方向时被调用 可以监听做处理 - (void)setArrowDirection:(UIPopoverArrowDirection)arrowDirection { } //这个方法在设置箭头偏移量时被调用 可以监听做处理 - (void)setArrowOffset:(CGFloat)arrowOffset { } @end ``` * 以下为绘制箭头图片 ``` - (UIImage *)drawArrowImage:(CGSize)size { UIGraphicsBeginImageContextWithOptions(size, NO, 0); CGContextRef ctx = UIGraphicsGetCurrentContext(); [[UIColor clearColor] setFill]; CGContextFillRect(ctx, CGRectMake(0.0f, 0.0f, size.width, size.height)); CGPoint point1; CGPoint point2; CGPoint point3; point1 = CGPointMake(size.width/2.0, 0); point2 = CGPointMake(size.width, size.height); point3 = CGPointMake(0, size.height); CGMutablePathRef arrowPath = CGPathCreateMutable(); CGPathMoveToPoint(arrowPath, NULL, point1.x, point1.y); CGPathAddLineToPoint(arrowPath, NULL, point2.x, point2.y); CGPathAddLineToPoint(arrowPath, NULL, point3.x, point3.y); CGPathCloseSubpath(arrowPath); CGContextAddPath(ctx, arrowPath); CGPathRelease(arrowPath); CGContextSetFillColorWithColor(ctx, self.bgColor.CGColor); CGContextDrawPath(ctx, kCGPathFill); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } ```