change popover viewcontroller cornerRadius in swift - ios

i am trying change cornerRadius of popUPViewController but not change
how to change that? at the same time i have another doubt also is it possible to change cornerRadius??

Your question seems too broad to help with a specific answer. I'm trying to help assuming what your needs are:
If you're referring to the new iOS 13 presentation style as "popUPViewController", you CANNOT change the corner radius of it
But, you can write your own custom transition adding your touches to it. This is a nice place to get started with.
And yes, you can easily change the corner radius of a view:
yourView.layer.cornerRadius = 10 //Whatever radius you'd want
yourView.layer.masksToBounds = true //This line is important. Doing this will restrain yourView's layer and sublayers to the cornered mask
You can additionally also specify corners to apply the radius with:
yourView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner] //This will round the top-left and top-right corners

try doing popupViewController.layer.cornerRadius = 10.0

Related

How to always get rounded corners (corner-radius) on a view that has dynamic height?

I'm rather new to iOS programming.
I was wondering what is the proper way to achieve permanently rounded corners (via the attribute view.layer.cornerRadius) for a view that has dynamic height.
In Android, we would just set the cornerRadius to an absurdly high number like 1000. This would result in the view always having rounded corners regardless of how tall or short it was.
Unfortunately, when I tried to do the same thing in iOS, I realized that an overly large value for cornerRadius results in the view being drawn in a distorted way - or straight up just disappearing from the layout altogether.
Anyone have any insights into this problem? Thanks.
Easy to achieve this with RxSwift by observing Key Path.
extension UIView{
func cornerHalf(){
clipsToBounds = true
rx.observe(CGRect.self, #keyPath(UIView.bounds))
.subscribe(onNext: { _ in
self.layer.cornerRadius = self.bounds.width * 0.5
}).disposed(by: rx.disposeBag)
}
}
Done in init method, the code seems simpler by declaration , instead of being distributed two parts. Especially, the logic is widely used in the project.
Call like this:
let update: UIButton = {
let btn = UIButton()
// more config
btn.cornerHalf()
return btn
}()

Gradient layer not in the right place

I have the following code as follows:
playView.layer.cornerRadius = 16
let gradient1 = CAGradientLayer()
gradient1.frame = playView.frame
gradient1.cornerRadius = 16
if #available(iOS 10.0, *) {
// set P3 colour
} else {
// set sRGB colour
}
gradient1.startPoint = CGPoint(x: 0, y: 0)
gradient1.endPoint = CGPoint(x: 1, y: 1)
playView.layer.insertSublayer(gradient1, at: 0)
On the 3rd line of the block of code above, I set the frame of the gradient equal to the frame I want it to fill.
When I run the app on different devices, the gradient layer will only fill the correct area if the device the app is being run on is the one selected in the Interface Builder.
I currently have the code in viewDidLoad(), and so the issue can be solved by moving the code to viewDidAppear(), but then when the app is loaded, there will be a slight delay before the gradient appears, not giving a smooth look and feel.
Is there another method I can put the code in, so that the gradient shows in the correct place, whilst at the same time being there as soon as the user sees the screen? Or alternatively, a way to make the gradient fill the view, whilst still keeping the code in viewDidLoad()?
EDIT: viewWillAppear() does not work, nor does viewWillLayoutSubviews(). Surely there must be away to solve this?
You can put inside this block:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
}
viewDidAppear() works. This screen is the root controller - I don't know if that makes a difference or not, but there is no visible delay on applying the gradient backgrounds.
I would be interested if anyone could explain this? I have a bar chart in another part of the app and in viewDidAppear() there is code to complete the bar chart, however there is a delay in it being filled in.
Change the layer.frame property inside the viewDidLayoutSubviews
method. This is to make sure that the subview (playView) has already a proper frame.

How can I bring a UIView's border behind a UIImage?

I'm new to Swift and I'm facing an issue while trying to develop a small application and I really hope someone will help me out.
Basically, I have a UIImage inside a UIView, and I want to send the UIView's border to the back (See picture below)
So, I'm giving the UIView a border, and it goes through my image. My question is:
How do I bring the border of the UIView behind the UIImage?
Note: I apologize if this is a repeated question, I was looking for answers for hours but couldn't find what I need.
Best regards!
Here is my code for the Image:
speakerImage.layer.cornerRadius = speakerImage.frame.size.width / 2;
speakerImage.clipsToBounds = true;
speakerImage.layer.zPosition = 1
And here is my code for the View:
speakerContentView.layer.cornerRadius = 5
speakerContentView.layer.borderWidth = 1
speakerContentView.layer.borderColor = UIColor.gray.cgColor
speakerContentView.layer.shadowColor = UIColor.gray.cgColor
speakerContentView.layer.shadowOpacity = 0.7
speakerContentView.layer.shadowOffset = CGSize(width: 0, height: 3)
speakerContentView.layer.zPosition = -1
This may help you!!
Move your image outside current (speakerContentView) view and set it in higher hierarchy so that it can get front position.
Set vertical center Y of image equal to current (speakerContentView) view Y
Look at this image:
May be not a standard way but will solve your problem.
You should add a subview with the same size of the superview below the image, and assign the border/shadow to it instead of its superview.

How to make UIButton a circle?

I have been trying to make the UIButton in the cells a perfect circle. Unfortunately the circle has been formed based on the background image rather than the UIButton frame.
The code I have for creating a circle:
cell.StoryViewButton.setImage(image, forState: .Normal)
cell.StoryViewButton.frame = CGRectMake(50, 8, 100, 100)
cell.StoryViewButton.layer.masksToBounds = false
cell.StoryViewButton.layer.cornerRadius = cell.StoryViewButton.frame.width/2
cell.StoryViewButton.clipsToBounds = true
The output looks like this:
What can I do to get the perfect circle button frames that I want?
Try something like this
cell.StoryViewButton.layer.masksToBounds = true
cell.StoryViewButton.layer.cornerRadius = cell.StoryViewButton.frame.width/2
If you need to create a circle of view you have to set masksToBounds to true, do not set clipsToBounds
Hope this will help you.
Swift 4
You could apply the following function to any view, including buttons.
func makeViewCircular(view: UIView) {
view.layer.cornerRadius = view.bounds.size.width / 2.0
view.clipsToBounds = true
}
Great.
That didn't work for me at first as I applied it in the viewDidLoad, though. At this point, constraints are still playing with the size of your buttons and the corner radius is applied to a button it thinks is twice the size, resulting in the odd shapes you have. To apply the right values to the right measurements, place the code in override func viewDidLayoutSubviews().
I know this may be a more specific case within my personal process, yet I'm sure it'll help somebody.
There is the working code.I test it. Hope it helps.
I am not sure why but I check that by changing height and width of the frame in here "CGRectMake(50, 8, 120, 120)" that the height and width of the frame and height and width of a button should be same to get the perfect circle.
cell.StoryViewButton.setImage(image, forState: .Normal)
cell.StoryViewButton.frame = CGRectMake(50, 8, 120, 120)
cell.StoryViewButton.layer.cornerRadius = self.btn.frame.width/2;
cell.StoryViewButton.layer.masksToBounds = true
Hope it helps.

issues with UIImageView.layer.cornerRadius to create rounded images on different pixel densities ios

I'm simply trying to create a perfectly round image. Here's my swift code:
myImage.layer.cornerRadius = myImage.frame.size.width/2
myImage.layer.masksToBounds = true
This works on a 4s, but is not quite round on a 5s, and appears as a rounded rectangle on a iphone 6.
I'm assuming this has to do with frame.size.width returning values in pixels not points or something like that, but I've been unable to solve this problem.
If you're putting that code in viewDidLoad, try moving it to viewDidLayoutSubviews.
I'm guessing it's an auto layout issue -- although you've used the corner radius property appropriately and are in fact making the image frame circular, after auto-layout, the corner radius stays the same, but the image stretches so that it's no longer circular.
If your code is in viewDidLoad in ViewController, try moving it to viewDidLayoutSubviews.
If your rounded imageView is in tableViewCell, try moving it to draw.
override func draw(_ rect: CGRect) {
avatarView.layer.cornerRadius = avatarView.frame.size.width / 2
avatarView.layer.masksToBounds = true
avatarView.clipsToBounds = true
avatarView.contentMode = .scaleAspectFill
}
The problem is that if you change the cornerRadius of Any view, and expect it to look like a circle, the view has to be a square.
Now, because of different devices and different device size, the size of your image view might change and it may be a rectangle.
For e.g.
If you view is a 50 * 50
myImage.layer.cornerRadius = myImage.frame.size.width/2
This would add corner radios of 25 on both sides to make it a circle.
But because of auto layout of device change, your view might be a 50 * 80
Corner radius would add a 25, but as the height is 80, it will add 25 on both sides, and the remaining 30 won't be a curve and look straight.
What you can do is try viewing the screen in various orientations in the storyboard and change auto layout Constraints (Or structs and springs) to ensure that the image view is a square in all the devices
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
myImage.layer.cornerRadius = myImage.frame.size.width/2
myImage.layer.masksToBounds = true
}
This should work:
myImage.layer.cornerRadius = myImage.**bounds**.size.width/2
myImage.layer.masksToBounds = true

Resources