通过原生UI控件在iOS中提供工具提示



如何在iOS中创建工具提示或类似的东西,而不使用任何第三方类?我有一个UIButton,我想有一个工具提示弹出几秒钟或直到它被清除。我看过第三方的类和库,但想知道它是否被本地支持。我还想显示一个箭头从工具提示的来源弹出。我见过一些UIActionSheet弹出窗口有这个箭头。

欢呼,Amit

我最终还是使用了第三方工具提示CMTopTipView。它的开销相对较低,只有一个头文件和实现。稍微修改了一下,以考虑ARC。以下是我所做的:

#import "CMPopTipView.h"
CMPopTipView *navBarLeftButtonPopTipView;
- (void) dismissToolTip
{
   [navBarLeftButtonPopTipView dismissAnimated:YES];
}
- (void) showDoubleTap
{
    navBarLeftButtonPopTipView = [[CMPopTipView alloc] 
       initWithMessage:@"DOUBLE Tap n to view details"] ;
    navBarLeftButtonPopTipView.delegate = self;
    navBarLeftButtonPopTipView.backgroundColor = [UIColor darkGrayColor];
    navBarLeftButtonPopTipView.textColor = [UIColor lightTextColor];
    navBarLeftButtonPopTipView.opaque = FALSE;
    [navBarLeftButtonPopTipView presentPointingAtView:catButton1 
        inView:self.view animated:YES];
    navBarLeftButtonPopTipView.alpha = 0.75f;
    NSTimer *timerShowToolTip = [NSTimer scheduledTimerWithTimeInterval:5.0 
     target:self 
     selector:@selector(dismissToolTip) userInfo:nil repeats:NO];
 }

如果你在iPad上,你可以使用UIPopoverView。你也有UIMenuController工作与"弹出窗口"像iPhone或iPad上的功能:教程。除此之外,你可以让自己的UIView子类做到这一点,但你必须自己处理箭头。

我最后做的相对简单。我最终使用了没有按钮的UIActionSheet,只有一个文本。然后使用了一个来自坐标平面的showFromRect, UIButton在self.view中。

UIActionSheet *popup = [[UIActionSheet alloc] 
initWithTitle:@"DOUBLE Tap n to view details." 
delegate:self cancelButtonTitle:@"Cancel" 
destructiveButtonTitle:nil otherButtonTitles: nil];
[popup sizeToFit];
popup.tag = 9999; 
CGRect myImageRect = CGRectMake(240.0f, 605.0f, 30.0f, -40.0f);
[popup showFromRect:myImageRect inView:self.view animated:YES];

我可能只是吸收和使用CMPopTipView(第三方控件)来调整它的大小和不透明度和褪色的alpha

我看到你们中的一些人正在使用CMPopTip,一个很棒的"库"。酷的方式!

只是一些东西,如果你在iOS7中使用它,你会有一些不赞成。

新增文本弃用部分(这是一个例子)

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = self.titleAlignment;
    [self.title drawInRect:titleFrame withAttributes:@{NSFontAttributeName:self.titleFont,NSParagraphStyleAttributeName:paragraphStyle}];

再见!

最新更新