简体   繁体   中英

How can I shape a subview to it's superview in swift?

I am trying to add a UIProgressView along the top of a UIView . There is a border and rounded corners on the UIView . Currently it looks likes this:

我的UIProgressBar和UIView

How can I get the UIProgressBar corners to follow those of the UIView ?

The UIProgressView is a subview of the UIView . I have tried setting clipsToBounds = true but it didn't work.

Thanks!

You need to add

yourView.layer.masksToBounds = true

And code will look like:

override func viewWillAppear(_ animated: Bool) {
    mainV.layer.masksToBounds = true
    mainV.layer.borderColor = UIColor.black.cgColor
    mainV.layer.borderWidth = 1.0
    mainV.layer.cornerRadius = 10
}

And result will be:

在此处输入图片说明

You can make progressView rounded by:

self.progressView.layer.cornerRadius = //some int

But clipsToBounds property forces view to not to draw anything outside of the view's bounds.

Check your viewController hierarchy and make sure your progressView is subView of your view not the main view of viewController.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM