【问题标题】:iPhone development: How to create colored or translucent background for UIActionSheet?iPhone 开发:如何为 UIActionSheet 创建彩色或半透明背景?
【发布时间】:2009-07-25 05:15:10
【问题描述】:

当您尝试在 iPhone 的笔记应用程序中删除笔记时,会弹出一个 UIActionSheet。该片材是半透明的(但不是黑色半透明的)。这是如何实现的?是否可以将 UIActionSheet 的背景设置为某种颜色?

【问题讨论】:

    标签: iphone uikit


    【解决方案1】:

    我通常实现以下委托方法:

    - (void)willPresentActionSheet:(UIActionSheet *)actionSheet
    

    只是为了制作样品。在这种情况下,我使用拉伸的 png 作为背景:

    - (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
        UIImage *theImage = [UIImage imageNamed:@"detail_menu_bg.png"];    
        theImage = [theImage stretchableImageWithLeftCapWidth:32 topCapHeight:32];
        CGSize theSize = actionSheet.frame.size;
        // draw the background image and replace layer content  
        UIGraphicsBeginImageContext(theSize);    
        [theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];    
        theImage = UIGraphicsGetImageFromCurrentImageContext();    
        UIGraphicsEndImageContext();
        [[actionSheet layer] setContents:(id)theImage.CGImage];
    }
    

    结果如下: alt text http://grab.by/4yF1

    【讨论】:

    • 您好,能否请您发布该图片的链接?它看起来真的很不错,谢谢和替换背景图片的绝佳解决方案!
    【解决方案2】:

    你可以使用下面的代码:

    actionSheetObj.actionSheetStyle=UIActionSheetStyleBlackOpaque;
    

    actionSheetObj.actionSheetStyle=UIActionSheetStyleBlackTranslucent;
    

    【讨论】:

    • 谢谢,这正是我想要的
    【解决方案3】:
    actionSheetObj.actionSheetStyle=UIActionSheetStyleBlackTranslucent;
    

    【讨论】:

      【解决方案4】:

      这并不难。您可以使用以下代码:

      CGSize mySize = myActionSheet.bounds.size;
      CGRect myRect = CGRectMake(0, 0, mySize.width, mySize.height);
      UIImageView *redView = [[[UIImageView alloc] initWithFrame:myRect] autorelease];
      [redView setBackgroundColor:[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.5]];
      [myActionSheet insertSubview:redView atIndex:0];
      

      请确保在执行此操作之前呈现 UIActionSheet,否则不会设置大小。这让它有点难看,但你可以这样做:

      [myActionSheet showInView:self.view];
      if (!redAdded) {
          redAdded = YES;
          //THE ABOVE CODE GOES HERE
      }
      

      【讨论】:

      • 如果您将大小绑定到视图,则不必执行后面的部分。还必须用高度和宽度替换 x 和 y: CGSize mySize = self.view.bounds.size; CGRect myRect = CGRectMake(0, 0, mySize.height, mySize.width);
      • 使用 - (void)willPresentActionSheet:(UIActionSheet *)actionSheet 通常是一个更好的钩子
      • 您也可以通过将 UIImage 设置为 UIImageView 来使用图像而不是颜色:[actionBackground setImage:[UIImage imageNamed:@"captainplanet.png"]];
      【解决方案5】:

      您绝对可以通过设置 Alpha 值来调整不透明度。 Interface Builder 允许您直接编辑值,但在代码中我认为您会这样做:

      [[myActionSheet view] setOpaque:NO]; 
      [[myActionSheet view] setAlpha:0.5];
      

      我不确定您是否需要 setOpaque 调用 - 我认为它用于帮助优化性能,因为 iPhone 不会尝试渲染不透明视图隐藏的任何内容。

      【讨论】:

        【解决方案6】:

        在我看来它是黑色的(注意:使用 2.2.1)。它有颜色的唯一原因是它背后的黄色。

        一种选择是使用黑色透明背景并找出操作表的大小和速度。然后创建一个视图并为其设置动画,同时在其下方显示操作表,使其具有与操作表后面的自然颜色不同的色调。您必须使颜色视图也半透明,这样您也可以看到其背后。

        我不确定你是否可以,但你也可以尝试调整操作表本身的不透明度。

        【讨论】:

        • 调整操作表的不透明度也会影响按钮。
        • 如果您查看便笺应用程序,您会注意到按钮并非 100% 不透明。
        猜你喜欢
        • 2019-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-17
        • 2011-11-15
        • 1970-01-01
        • 2020-07-21
        • 2011-06-11
        相关资源
        最近更新 更多