Top left right corner radius uiview swift stackoverflow năm 2024

Pay attention to the fact that if you have layout constraints attached to it, you must refresh this as follows in your UIView subclass:

override func layoutSubviews() {

super.layoutSubviews()  
roundCorners(corners: [.topLeft, .topRight], radius: 3.0)  
}

If you don’t do that it won’t show up.

extension UIView { func roundCorners(corners: UIRectCorner, radius: CGFloat) {

    let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))  
    let mask = CAShapeLayer()  
    mask.path = path.cgPath  
    layer.mask = mask  
}  
}

I have created a Grouped TableView dynamically based on data. Based on data tableView cell generated automatic height so every cell has different rowHeight. I have set it accordingly by using self.tableView.rowHeight = 50

But Issue is I am using corner radius, but I don't want to use corner radius on every cell. I am using grayBox UIView and all cells displayed in it. Corner radius apply to start of cell or grayBox and only at end of cell of grayBox but it applied to every cell. How can I do that corner radıus apply on start and bottom?

viewDidLoad() code for tableView Row Height

self.tableView.rowHeight = 50

Dynamic Grouped TableView code:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()      
        cell.backgroundColor = UIColor.clear
        cell.selectionStyle = .none
        let grayBox = UIView(frame: CGRect(x: 5, y: 0, width: self.view.frame.size.width - 11, height: 50))
        grayBox.backgroundColor = ("
# cfd8dc").toColor()
        grayBox.layer.cornerRadius = 5
        grayBox.layer.borderColor = UIColor(red:0.80, green:0.80, blue:0.80, alpha:1.0).cgColor
        grayBox.layer.borderWidth = 1.0
        cell.contentView.addSubview(grayBox)
        return cell
    }

I’ve been working on an application for a couple of years and received a simple design request: Round the corners on a view and add a drop shadow. Easy right?

// set the corner radius layer.cornerRadius = 6.0 layer.masksToBounds = true// set the shadow properties layer.shadowColor = UIColor.black.cgColor layer.shadowOffset = CGSize(width: 0, height: 1.0) layer.shadowOpacity = 0.2 layer.shadowRadius = 4.0

WRONG! If you’ve tried this before, you know exactly what happens. The corners will be rounded, but the shadow will be missing. If you set masksToBounds to false, the shadow will appear, but the corners will not be rounded.

CALayerWhyAreYouDoingThisToMe

The reason why is actually a bit obvious in hindsight, layer.masksToBounds = true clips everything outside of the layer. A shadow will be drawn outside of the layer, thus it is clipped, too. So, we cannot use the same layer for both of these effects.

The Solution

I’ll share what I found to be the path of least resistance. I opted to create an inner containerView pinned to the edges of the parent view. The shadow is applied to the parent view’s layer, while the rounded corners are applied to the containerView. Then, just add all content to the containerView and be on your way.

I’m certain this can also be accomplished using sublayers and masks, but this has some friction when using AutoLayout since we don’t know the size of the view when it’s laid out. That would require overriding layoutSubviews in the view or viewDidLayoutSubviews in the view controller in order to update the layer paths, but frankly, that’s more effort than it is worth for something so seemingly trivial.

Checkout the gist below. It’s well commented, so it should be easy to follow.

The Result

Update!

I stumbled on a StackOverflow post (of course) and found an alternative solution that you can use to avoid adding subviews. The magic happens with:`masksToBounds`0

The layoutSubviews method

If you have other solutions or suggestions, I would love to hear them! Thanks for reading and I hope this helps!

How to set top corner radius for view in Swift?

cornerRadius() modifier and specify the corners we want to apply the radius to. In the code snippet above, we extend the View type and add a method called cornerRadius() that takes two parameters: radius for the corner radius value, and corners for specifying the corners to apply the radius to.

How do you round the top corner in SwiftUI?

In SwiftUI, there is a convenient built-in modifier called cornerRadius that allows you to easily create rounded corners for a view. By applying the cornerRadius modifier to a Rectangle view, you can transform it into a rounded rectangle.

How to add rounded border to uiview in Swift?

One common way is to use the cornerRadius property of the layer object. The cornerRadius property sets the radius of the view's corners, and can be used to create a rounded rectangle or oval shape. This will create a circular view with a diameter equal to the width of the view.