UIWindow在整个应用程序中圆角

时间:2012-09-08 17:34:54

标签: ios cocoa window rounded-corners mask

我想在我的应用程序边缘(整个应用程序)周围留下小圆角,所以我认为最好的位置是在{{1}创建的主UIWindow层上}类。这是我的代码......

AppDelegate

这会绕窗口底部的角落,稍微在顶部。然而,窗口是屏幕的完整大小(不是屏幕减去状态栏)所以我最终得到这样的东西.. http://tfld.me/image/441U0j3w3X3N

我真的不想在我的应用程序中的所有导航控制器/视图的每一层上添加cornerRadius / masksToBounds选项..或者有一个背景图像(因为这不是将来的证据)。

我也试过在windows rootViewController上设置这些选项而没有运气 - http://tfld.me/image/0c1m2w36402K

任何建议都非常感激:)

3 个答案:

答案 0 :(得分:2)

如果您希望导航栏弯曲,可以使用合适的图像。如果没有,则以下代码应根据您的需要运行:

    self.window = [[[UIWindow alloc] init] autorelease];
    //shift the window frame by 20 px so that it goes below status bar
    CGRect sampleRect = [[UIScreen mainScreen] bounds];
    sampleRect.origin.y += 20.0;
    sampleRect.size.height -= 20.0;
    self.window.frame = sampleRect;

    UIViewController *vc = [[UIViewController alloc] init];
    vc.view.frame = [[UIScreen mainScreen] bounds];

    UINavigationController *mRootController = [[UINavigationController alloc] initWithRootViewController:vc];
    CGRect navFrame = mRootController.view.frame;
    navFrame.origin.y -= 20.0; //Shift the navigation frame up by 20 px
    mRootController.view.frame = navFrame;

    [self.window addSubview:mRootController.view];
    [self.window.layer setCornerRadius:20.0f];
    [self.window.layer setMasksToBounds:YES];

答案 1 :(得分:0)

要做到这一点,我使用了一个小的png文件,其中包含1/4的透明圆圈(其半径将是窗口的新半径),黑色朝向角落(设计师为我提供了图像,你可能有自己动手)。我使用此png创建了4个imageView,相应地轮换它们并将它们作为子视图添加到navigationController

将以下方法放入appDelegate并在初始化application:didFinishLaunchingWithOptions后在navigationController:方法内调用。

-(void) insertCornerRadiiOnNavigationController : (UINavigationController *) navController{

CGFloat cornerRadius = 7;

UIImageView *corner1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, cornerRadius, cornerRadius)];
UIImageView *corner2 = [[UIImageView alloc] initWithFrame:CGRectMake([_window bounds].size.width - cornerRadius, 20, cornerRadius, cornerRadius)];
UIImageView *corner3 = [[UIImageView alloc] initWithFrame:CGRectMake(0, [_window bounds].size.height - cornerRadius, cornerRadius, cornerRadius)];
UIImageView *corner4 = [[UIImageView alloc] initWithFrame:CGRectMake([_window bounds].size.width - cornerRadius, [_window bounds].size.height - cornerRadius, cornerRadius, cornerRadius)];

corner2.transform = CGAffineTransformMakeRotation (M_PI_2);
corner3.transform = CGAffineTransformMakeRotation (3 * M_PI_2);
corner4.transform = CGAffineTransformMakeRotation (M_PI);

[corner1 setImage:[UIImage imageNamed:@"corner.png"]];
[corner2 setImage:[UIImage imageNamed:@"corner.png"]];
[corner3 setImage:[UIImage imageNamed:@"corner.png"]];
[corner4 setImage:[UIImage imageNamed:@"corner.png"]];

[navController.navigationBar addSubview:corner1];
[navController.navigationBar addSubview:corner2];
[navController.view addSubview:corner3];
[navController.view addSubview:corner4];

[corner1 release];
[corner2 release];
[corner3 release];
[corner4 release];
}

出于某种原因,我无法直接将其添加到UIWindowimageViews没有出现。

答案 2 :(得分:0)

迅速4:

Script runtime