0

I have a custom button that initialized with a custom image of on top corner. I am not able to bring that imageview to the front;

enter image description here Hierarchy; enter image description here

Initializing with the badge as;

- (instancetype)initWithCoder:(NSCoder *)coder{
    self = [super initWithCoder: coder];
    if (self) {
        self.imageEdgeInsets = UIEdgeInsetsMake(0, -14, 0 , 0);
        _checkedBagde = [[UIImageView new]initWithImage:[UIImage imageNamed:@"checkedBadge"]];
        _checkedBagde.frame = CGRectMake(-10, -5, 24, 15);
        [self addSubview:_checkedBagde];
        [self bringSubviewToFront: _checkedBagde];
    }
    return self;
}

On view controller I am using below function (in viewWillAppear) to set my custom button declared in button's class.

- (id)setButton:(DayButtonStates)buttonType
   relatedArray:(NSArray *)relatedArray
    isCompleted:(BOOL)isCompleted{
    
    if (self) {
        [_checkedBagde setHidden: isCompleted ? NO : YES];
        //[self setTitle: relatedArray.title forState: normal];
        self.layer.cornerRadius = 4;
        self.relatedArray = relatedArray;
        switch (buttonType) {
            case activeRemoteButton:
                [self setActiveRemoteButton:self];
                break;
            case passiveRemoteButton:
                [self setPassiveRemoteButton:self];
                break;
            case activeOnSiteButton:
                [self setActiveOnsiteButton:self];
                break;
            case passiveOnSiteButton:
                [self setPassiveOnsiteButton:self];
                break;
        }
    }
    return self;
}

For example, setPassiveOnsiteButton function is as below :

- (void)setPassiveOnsiteButton {
    [self setButtonType:passiveOnSiteButton];
    UIImage *passiveOnsiteIcon = [UIImage imageNamed:@"iconBusP"];
    self.layer.borderWidth = 1;
    [self bringSubviewToFront: self.checkedBagde];
    self.layer.borderColor = [RGB(63, 206, 193) CGColor];
    self.backgroundColor = RGB(54, 54, 54);
    [self setSelected:NO];
    [self setImage: passiveOnsiteIcon forState:UIControlStateNormal];
    [self setTitleColor: RGB(63, 206, 193) forState:UIControlStateNormal];
}

As you see in first picture the button layer shows up front. Because of assigning the border to the button after bringing front the badge. How can I send the related border back? Thank you.

Kaan Vural
  • 35
  • 9
  • Add a UIView subview that extends the bounds of the button below your check in the button. Add the border to that view and the check will be above. What you are stating is the correct behavior of the layer border. – agibson007 Sep 05 '20 at 11:56
  • It is possible. You have control of the subviews you add. Before this line addSubview: checkBadge add a UIView. Make it exactly equal to the frame of the button. Add the border to that and not the button layer. – agibson007 Sep 05 '20 at 12:24
  • But I would do the above by adding another subview below the checkBadge and drawing the border on that. – agibson007 Sep 05 '20 at 12:35

0 Answers0