2

我想在我的应用程序边缘(整个应用程序)周围有小圆角,所以我认为最好的地方是在课堂上UIWindow创建的 main 层。AppDelegate这是我的代码...

[self.window.layer setCornerRadius:30.0f];
[self.window.layer setMasksToBounds:YES];

这确实绕过了我窗口底部的角落,并且稍微在顶部。但是窗口是屏幕的全尺寸(不是屏幕减去状态栏)所以我最终得到这样的东西.. http://tfld.me/image/441U0j3w3X3N

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

我也尝试在 Windows rootViewController 上设置这些选项,但没有成功 - http://tfld.me/image/0c1m2w36402K

任何建议都非常感谢:)

4

3 回答 3

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];
于 2012-09-10T09:53:28.120 回答
1

斯威夫特 4:

    func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        window.layer.cornerRadius = 20.0
        window.layer.masksToBounds = true
于 2019-06-18T03:05:06.687 回答
0

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

将以下方法放入您的并在: 初始化后appDelegate调用它。application:didFinishLaunchingWithOptionsnavigationController

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

由于某种原因,我无法UIWindow直接将它们添加到,imageViews没有出现。

于 2013-03-26T17:22:20.620 回答