UIView with rounded corners and drop shadow?

前端 未结 30 2617
一整个雨季
一整个雨季 2020-11-22 08:00

I’ve been working on an application for a couple of years and received a simple design request: Round the corners on a UIView and add a drop shadow.To do as given below.

30条回答
  •  醉话见心
    2020-11-22 08:36

    You need to use two UIViews to achieve this. One UIView will work like shadow and other one will work for rounded border.

    Here is a code snippet a Class Method with a help of a protocol:

    @implementation UIMethods
    
    + (UIView *)genComposeButton:(UIViewController  *)observer;
    {
        UIView *shadow = [[UIView alloc]init];
        shadow.layer.cornerRadius = 5.0;
        shadow.layer.shadowColor = [[UIColor blackColor] CGColor];
        shadow.layer.shadowOpacity = 1.0;
        shadow.layer.shadowRadius = 10.0;
        shadow.layer.shadowOffset = CGSizeMake(0.0f, -0.5f);
    
        UIButton *btnCompose = [[UIButton alloc]initWithFrame:CGRectMake(0, 0,60, 60)];
        [btnCompose setUserInteractionEnabled:YES];
        btnCompose.layer.cornerRadius = 30;
        btnCompose.layer.masksToBounds = YES;
        [btnCompose setImage:[UIImage imageNamed:@"60x60"] forState:UIControlStateNormal];
        [btnCompose addTarget:observer action:@selector(btnCompose_click:) forControlEvents:UIControlEventTouchUpInside];
        [shadow addSubview:btnCompose];
        return shadow;
    }
    

    In the code above btnCompose_click: will become a @required delegate method which will fire on the button click.

    And here I added a button to my UIViewController like this:

    UIView *btnCompose = [UIMethods genComposeButton:self];
    btnCompose.frame = CGRectMake(self.view.frame.size.width - 75,
                              self.view.frame.size.height - 75,
                              60, 60);
    [self.view addSubview:btnCompose];
    

    The result will look like this:

    enter image description here

提交回复
热议问题