I tried rotating a UIView using:
view.transform = CGAffineTransform.init(rotationAngle: 45)
only to see the edges of the view looked very distorted.
Is this normal? And if it is, are there any workarounds to straighten the edges?
EDIT
I found that adding the piece of code below works the best
view.layer.allowsEdgeAntialiasing = true
refer this link,
https://stackoverflow.com/a/8313978/6742121,
After that implement this for swift 3.0,
view.layer.borderWidth = 3
view.layer.borderColor = UIColor.clear.cgColor
view.layer.shouldRasterize = true
view.layer.rasterizationScale = UIScreen.main.scale()
Related
I have a custom popup view in my iOS application and inside that there are two views who need a small shadow below them. I'm animating this popup from the bottom to the top of the screen. While animating it flickers/stutters because of the shadows. Do you have any idea how to fix this?
Here is the code that I'm using for the shadows (Swift 3):
view.layer.masksToBounds = false
view.layer.shadowColor = UIColor.black.cgColor
view.layer.shadowOffset = CGSize(width: 0, height: 1)
view.layer.shadowRadius = 1
view.layer.shadowOpacity = 0.5
And yes, it is definitely because of the shadows.
Question I'm struggling on. I've searched SO for a fair good time by now, but couldn't find an answer.
I have a UIView which contains alpha value of 0.5, means - it transparent.
If I'm applying the usual code for UIView shadowing -
class func applyShadow(view : UIView)
{
view.layer.shadowColor = UIColor.blackColor().colorWithAlphaComponent(0.15).CGColor
view.layer.shadowOpacity = 1
view.layer.shadowOffset = CGSizeMake(0, 0.5)
view.layer.shadowRadius = 1.3
view.layer.masksToBounds = false
view.layer.shouldRasterize = true
view.layer.rasterizationScale = UIScreen.mainScreen().scale
}
The "fill" of the my UIView get shadowed as well.
How can I draw the shadow only on the "border" path of my UIView, excluding the UIView fill?
For 2022 this is now possible.
Essentially you do this:
shadowHole.fillRule = .evenOdd
shadowHole.path = p.cgPath
full details ...
https://stackoverflow.com/a/59092828/294884
I have a UIScrollView that pretty much functions like a Facebook news feed. I thought my elements were slowing the scroll fps down. By process of elimination, I found out that the shadows slow down my app.
The UIView boxes inside the scroll view have such configuration:
self.layer.shadowColor = UIColor.blackColor().CGColor
self.layer.shadowOffset = CGSizeMake(0, 2)
self.layer.shadowRadius = 2
self.layer.shadowOpacity = 0.15
Like a news feed, my scroll view has many boxes, therefore having UIViews with their own shadows. How do I go with this without slowing down my app?
There's a bunch of stuff on speeding up UIScrollViews:
CALayer - Shadow causes a performance hit?
https://markpospesel.wordpress.com/2012/04/03/on-the-importance-of-setting-shadowpath/
If you use custom CALayer instances -- especially with shadows -- stuff that requires processing power, you should use
scrollView.layer.shouldRasterize = YES;
scrollView.layer.rasterizationScale = [UIScreen mainScreen].scale;
Also a shadowPath could speed up your scrollview as well something like this:
[scrollView.layer setShadowPath:[[UIBezierPath bezierPathWithRect:myView.bounds] CGPath]];
Setting a shadowPath allows iOS to not recalculate how it should draw the shadow every time it draws the view, thus improving performance.
In swift, something like this:
let shadowPath = UIBezierPath(rect: view.bounds);
view.layer.masksToBounds = false;
view.layer.shadowColor = UIColor.blackColor().CGColor;
view.layer.shadowOffset = CGSizeMake(0, 0.5);
view.layer.shadowOpacity = 0.2;
view.layer.shadowPath = shadowPath.CGPath;
Setting the shadowPath will improve performance and look the same so long as your views are opaque. You could just set it to the bounds of the view or layer like so:
CGPathRef shadowPath = CGPathCreateWithRect(self.bounds, NULL);
self.layer.shadowPath = shadowPath;
CGPathRelease(shadowPath);
In Swift:
layer.shadowPath = CGPathCreateWithRect(bounds, nil)
The memory management is handled for you (discussed here).
I'm trying to apply round corners to an UIImageView. Although the corners are round, there are still 4 edges, which simply won't go away no matter the size of the radius. Removing the border also doesn't help.
Could this have something to do with auto layout constraints? What am I doing wrong?
Here's the code I'm applying:
self.imageViewProfilePicture.layer.cornerRadius = self.imageViewProfilePicture.frame.size.width / 2.0
self.imageViewProfilePicture.layer.borderWidth = 2.0
self.imageViewProfilePicture.layer.borderColor = UIColor.whiteColor().CGColor
self.imageViewProfilePicture.layer.masksToBounds = true
self.imageViewProfilePicture.clipsToBounds = true
Link to the image
As you are using constraints to define your imageView's width and height, the final frame will be defined after the layout of the subviews. In your case, just move your code to viewDidLayoutSubviews:
- (void)viewDidLayoutSubviews
{
super.viewDidLayoutSubviews()
self.imageViewProfilePicture.layer.cornerRadius = self.imageViewProfilePicture.frame.size.width / 2.0
self.imageViewProfilePicture.layer.borderWidth = 2.0
self.imageViewProfilePicture.layer.borderColor = UIColor.whiteColor().CGColor
self.imageViewProfilePicture.layer.masksToBounds = true
self.imageViewProfilePicture.clipsToBounds = true
}
Try setting your borderWidth to 0.
I've noticed that when I set a color for UISegmentedControl.backgroundColor, the color bleeds beyond the edges of the control (though not beyond the view's bounds). Here's an example with the segmented control's background color set to white and the container view's background color set to gray:
I've set the AutoLayout constraints of the segmented control such that the intrinsicContentSize should be used, but I haven't seen anyone else posting about this problem
Note that the image above is the best I've been able to get it to look... before that it was bleeding over by about 3-4px.
I've tried configuring the view to clipSubviews and the layer backing the UIView to masksToBounds, but I didn't expect that to fix the problem since I assume the bleeding is contained inside the view's/layer's bounds.
Any suggestions or advice appreciated. If not I'll just have to create images to back the UISegmentedControl that fix the bleeding, but that's annoying to have to maintain, to say the least.
Set the segment control's layer's corner radius to 4.0. It should help. You may need to import QuartzCore to be able to access the layer's properties.
segment.layer.cornerRadius = 4.0;
segment.clipsToBounds = YES;
Set segment control layer corner radius to 5. and ClipsToBounds YES .
segmentController.layer.cornerRadius = 5;
segmentController.clipsToBounds = YES;
Hope its work for you
the best result I could achieve in Swift:
segmentedControl.layer.cornerRadius = 4
let mask = CAShapeLayer()
mask.frame = CGRectMake(0, 0, segmentedControl.bounds.size.width-1, segmentedControl.bounds.size.height);
let maskPath = UIBezierPath(roundedRect: mask.frame,
byRoundingCorners: [.BottomLeft, .BottomRight, .TopLeft, .TopRight],
cornerRadii: CGSize(width: 4.0, height: 4.0))
mask.path = maskPath.CGPath
segmentedControl.layer.mask = mask