繁体   English   中英

iPhone开发:如何为UIActionSheet创建彩色或半透明背景?

[英]iPhone development: How to create colored or translucent background for UIActionSheet?

当您尝试删除iPhone的Notes应用程序中的注释时,会弹出一个UIActionSheet。 片材是半透明的(但不是黑色半透明)。 这是如何实现的? 是否有可能使UIActionSheet的背景成为某种颜色?

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

- (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

您可以使用以下代码:

actionSheetObj.actionSheetStyle=UIActionSheetStyleBlackOpaque;

要么

actionSheetObj.actionSheetStyle=UIActionSheetStyleBlackTranslucent;
actionSheetObj.actionSheetStyle=UIActionSheetStyleBlackTranslucent;

这不是太困难。 您可以使用以下代码:

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
}

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

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

我不确定你是否需要setOpaque调用 - 我认为这有助于优化性能,因为iPhone不会尝试渲染隐藏在不透明视图中的任何东西。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM