简体   繁体   English

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

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

When you try deleting a note in iPhone's Notes application, an UIActionSheet pops up. 当您尝试删除iPhone的Notes应用程序中的注释时,会弹出一个UIActionSheet。 The sheet is translucent (but not black translucent). 片材是半透明的(但不是黑色半透明)。 How is that achieved? 这是如何实现的? Is it possible to make the background of UIActionSheet a certain color? 是否有可能使UIActionSheet的背景成为某种颜色?

I usually implement the following delegate method: 我通常实现以下委托方法:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet

Just to make a sample. 只是为了做一个样本。 In this case I use a stretched png as a background: 在这种情况下,我使用拉伸的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];
}

and this is the result: alt text http://grab.by/4yF1 这就是结果: alt text http://grab.by/4yF1

You can use the code below: 您可以使用以下代码:

actionSheetObj.actionSheetStyle=UIActionSheetStyleBlackOpaque;

or 要么

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

It's not too difficult. 这不是太困难。 You can use the following code: 您可以使用以下代码:

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];

Just make sure you present the UIActionSheet before doing this or the size wont be set. 只需确保在执行此操作之前提供UIActionSheet,否则将无法设置大小。 That makes it kind of ugly, but you could do something like: 这有点难看,但你可以这样做:

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

You can definitely adjust the opacity by setting the alpha value. 您可以通过设置Alpha值来调整不透明度。 Interface Builder lets you edit the value directly, but in code I think you would do: Interface Builder允许您直接编辑值,但在代码中我认为您会这样做:

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

I'm not sure if you need the setOpaque call or not - I think that is used to help optimize performance, as the iPhone won't try to render anything hidden by the opaque view. 我不确定你是否需要setOpaque调用 - 我认为这有助于优化性能,因为iPhone不会尝试渲染隐藏在不透明视图中的任何东西。

It looks black to me (note: using 2.2.1). 它对我来说看起来很黑(注意:使用2.2.1)。 The only reason there's a color to it is because of the yellow behind it. 有颜色的唯一原因是它背后的黄色。

One option would be to use the black transparent background and find out the size and speed of the action sheet. 一种选择是使用黑色透明背景并找出活动表的大小和速度。 Then create a view and animate it in at the same time you show the action sheet, just underneath it, to give it a tint different than the color naturally behind the action sheet. 然后在显示操作表的同时创建一个视图并为其设置动画,就在它下面,给它一个不同于操作表后面的颜色的色调。 You would have to make the color view also translucent so you could see behind that as well. 您必须使颜色视图也是半透明的,这样您也可以在后面看到它。

I'm not sure if you can, but you might also try adjusting the opacity of the action sheet itself as well. 我不确定你是否可以,但你也可以尝试调整动作表本身的不透明度。

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

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