【问题标题】:UIBarButtonItem with rounded corners and shadow带有圆角和阴影的 UIBarButtonItem
【发布时间】:2014-05-27 10:05:45
【问题描述】:

我希望UIBarButtonItem 上有圆角和阴影。

UIButton *useButton = [UIButton buttonWithType:UIButtonTypeCustom];

然后我尝试设置阴影和圆角:

useButton.layer.masksToBounds = NO;
useButton.layer.cornerRadius = 4;
useButton.layer.shadowOffset = CGSizeMake(0, 1.5);
useButton.layer.shadowRadius = 0.5;
useButton.layer.shadowOpacity = 1.0;
useButton.layer.shadowColor = [BSTCMColorUtility colorFromHexString:@"#AFAFAF"].CGColor;

最后我做了UIBarButtonItem:

UIBarButtonItem *useItem = [[UIBarButtonItem alloc] initWithCustomView:useButton];
[self.navigationItem setRightBarButtonItems:@[useItem]];

没有圆角,但有阴影。

如果我添加:

useButton.clipsToBounds = YES;

和/或:

useButton.layer.masksToBounds = YES;

我得到圆角,但没有阴影。所以我想我会尝试添加一个子层。

CALayer *useButtonShadowLayer = [CALayer new];
useButtonShadowLayer.frame = useButton.frame;
useButtonShadowLayer.cornerRadius = 4;
useButtonShadowLayer.backgroundColor = [UIColor whiteColor].CGColor;
useButtonShadowLayer.shadowOffset = CGSizeMake(0, 1.5);
useButtonShadowLayer.shadowRadius = 0.5;
useButtonShadowLayer.shadowOpacity = 1.0;
useButtonShadowLayer.shadowColor = [BSTCMColorUtility colorFromHexString:@"#AFAFAF"].CGColor;

useButton.layer.cornerRadius = 4;
useButton.layer.masksToBounds = YES;

UIView *parent = useButton.superview;
[parent.layer insertSublayer:useButtonShadowLayer below:useButton.layer];

它似乎不起作用,我看不到阴影。我得到了圆角。

UIBarButtonItem/UIButton 上不可能有圆角和阴影吗?

【问题讨论】:

    标签: ios objective-c cocoa-touch uibutton


    【解决方案1】:

    试试这个更新的代码:

    UIButton *useButton = [UIButton buttonWithType:UIButtonTypeCustom];
    useButton.frame = CGRectMake(100, 430, 100, 40);
    useButton.layer.masksToBounds = NO;
    useButton.layer.cornerRadius = 10;
    useButton.layer.shadowOffset = CGSizeMake(1.5, 1.5);
    useButton.layer.shadowRadius = 0.5;
    useButton.layer.shadowOpacity = 1.0;
    useButton.layer.shadowColor = [UIColor blackColor].CGColor;
    useButton.backgroundColor = [UIColor redColor];
    
    UIBarButtonItem *useItem = [[UIBarButtonItem alloc] initWithCustomView:useButton];
    [self.navigationItem setRightBarButtonItems:@[useItem]];
    

    【讨论】:

    • 自定义类型。就在问题中,第一行代码
    • 在我第一个版本的问题中,我忘了提到我使用按钮来制作 UIBarButtonItem。也许这就是问题所在......
    • 它有效,但当我使用按钮制作 UIBarButtonItem 时无效。我忘了在我的第一个版本的问题中提到这一点。
    • 谢谢,我不确定我的错误在哪里,我将你的所有代码粘贴到一个新项目中,它在那里工作。所以我的项目中的其他地方一定有错误:)
    • 我不明白这是如何回答这个问题的。如果将masksToBounds 设置为NO,则圆角半径不再可见。
    【解决方案2】:
        UIButton *useButton = [UIButton buttonWithType:UIButtonTypeCustom];
        useButton.frame = CGRectMake(0, 0, 60, 30);
        useButton.backgroundColor = [UIColor redColor];
        useButton.layer.masksToBounds = NO;
        useButton.layer.cornerRadius = 4;
        useButton.layer.shadowOffset = CGSizeMake(0, 1.5);
        useButton.layer.shadowRadius = 5;
        useButton.layer.shadowOpacity = 1.0;
    //    useButton.layer.shadowColor = [UIColor blackColor].CGColor;
        useButton.layer.borderColor = [UIColor blackColor].CGColor;
        [self.view addSubview:useButton];
    
        UIBarButtonItem *useItem = [[UIBarButtonItem alloc] initWithCustomView:useButton];
        [self.navigationItem setRightBarButtonItems:@[useItem]];
    

    【讨论】:

    • 谢谢。一定是我实际上是在 UIBarButtonItem 中“包装”了 UIButton 以在我的导航栏中设置右栏按钮项。
    【解决方案3】:
    btnname.layer.cornerRadius = 8.0;
    btnname.layer.shadowOffset = CGSizeMake(2, 2);
    

    【讨论】:

      【解决方案4】:
      btn.layer.cornerRadius = 2.0 ;
      

      【讨论】:

        【解决方案5】:
        buttonName.layer.cornerRadius = 2;
        buttonName.layer.borderWidth = 1;
        buttonName.layer.borderColor = [UIColor blacColor].CGColor;
        

        【讨论】:

          猜你喜欢
          • 2011-06-12
          • 2017-03-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多