Circular UIImageView is square on some devices - ios

I have a UIView that has a UIImageView in it.
I set the UIImageView to be circular and add a border to it like this:
self.profilePicImageView.layer.cornerRadius = self.profilePicImageView.frame.size.height / 2
self.profilePicImageView.layer.masksToBounds = true
self.profilePicImageView.layer.borderColor = UIColor.white.cgColor
self.profilePicImageView.layer.borderWidth = 3
It's being called after I initialize the view in my viewController and before I add it as a subview (function called setupUI()).
It works perfectly fine on most of the devices, but on the large screen devices (iPhone 6/6s/7 Plus and iPhone X) I get the border right but the image itself isn't circular.
Please see the examples:
Regular iPhones:
Large iPhones (iPhone 6/6s/7 Plus and iPhone X):
Any idea what the problem can be and how can I fix it?
Thanks!!

Just try this code it will help you.
imageView.layer.cornerRadius = imageHeight/2
imageView.layer.masksToBounds = true
imageView.contentMode = .scaleAspectFill

Related

Add shadow to image in UIKit

I have this screen with an UIImageView (the image is a SF Symbol)
The user can see this icon clearly, white on black. But - if the background is different:
It may be hard to see it.
I want to add a black shadow to the image so the user can see it better.
I tried looking online but all I saw is how to add shadow to the image view box, and that is not what I need. I need the shadow around the icon itself and not around the box of the image.
Any ideas? thanks in advance
Try this:
imageView.layer.shadowColor = UIColor.black.cgColor
imageView.layer.shadowRadius = 3.0
imageView.layer.shadowOpacity = 1.0
imageView.layer.shadowOffset = CGSize(width: 4, height: 4)
imageView.layer.masksToBounds = false

Screen hight with (UIScreen) is fine in iPad but wrong in iPhone

I am developing a game where a background is moving at the back. I sat the background hight using the below code. It is working fine with iPads but with iPhones it is not taking the proper hight of the screen; instead it is in the middle of the screen with almost half the hight of the screen! The application is only working in portrait orientation. This is for xCode 8 & SWIFT 3
Please check below the code I am using for the background
hight:
let screenSize: CGRect = UIScreen.main.fixedCoordinateSpace.bounds
background1.size.height = screenSize.height
Thanks in advance
I have resolved the issue but using a (workaround) rather than resolving the root cause as I could not identify it.
First I have noticed that when sizing the hight of the screen I have 2 conditions:
below code will work with all iPhones but not iPads:
background1.size.height = self.frame.height
below code will work with all iPads but not iPhones:
background1.size.height = screenSize.height
So since the minimum hight of screen of iPads is 1024; I used the below code to first check if it is iPad use condition 2 and if iPhone use condition 1 as follow:
let screenSize: CGRect = UIScreen.main.bounds
if screenSize.height < CGFloat(1024) {
background1.size.height = self.frame.height ////for iphone
} else {
background1.size.height = screenSize.height ////for ipad
}
I am not sure what your case is.But it happens in iPhone 5+ devices due to launch screen size.Normally it behaves like iPhone 5 screen size even with iPhone 6,iPhone 6+.
Now the soultion.
1.You can use the default launch screen storyboard to avoid the problem.
2.You have to add every sizes for iPhone screen size (320*480,640*960,640*1136,768*1334,1242*2208).please add also iPhone 7,7+ sizes too.
3.You can use asset Catalogs for launch screen.
If thats the case for you please use above solution.It should work.

iPhone 7 Plus AVPlayer has border around it (Colors mismatch on white)

I'm seeing strange behavior on the iPhone 7 Plus and iPhone 6 Plus. This doesn't happen on the simulator, only the physical device.
If you have an AVPlayer (Video has a white background) and the view to which it is attached has a white background (audio player is smaller than parent view) a border will appear around the AVPlayer.
The goal to do this was to blend the video into the background to create a cool effect. Its working great on every device except the physical Plus model devices.
My best guess is there is some perfect white difference. Does anyone know how to fix this or avoid this?
I had this exact problem and my solution was to add the AVPlayerLayer inside an UIView container and adding a mask onto the playerLayer with 1pt inset.
override func layoutSubview() {
super.layoutSubviews()
// .. sets frame to players source size
let maskLayer = playerLayer.mask ?? CALayer()
maskLayer.frame = playerLayer.bounds.insetBy(dx: 1, dy: 1)
maskLayer.backgroundColor = UIColor.white.cgColor
playerLayer.mask = maskLayer
}

Unexpected NSLayoutConstraint behaviour in IOS7

I am getting this issue, only in iOS versions prior to 8
The selected view in the very first screen shot is a UIImageView. you can also see the constraints i have connected. I need to make the image view circular, so i am using layer.cornerRadius property. normally for circular UIImageView we use cornerRadius = height/2.0
but when i am setting this property in "viewDidLayoutSubviews" method. some how its giving me a square imageview.
as you can see i have set aspect ratio of image 1:1
so it can be turned into circular UIImageView.
I know I must be missing out some facts about NSLayoutConstraints. please help me out
My Code :-
-(void)viewDidLayoutSubviews
{
float height = profile_InfoSubView_ProfilePic.bounds.size.height;
float width = profile_InfoSubView_ProfilePic.bounds.size.width;
NSLog(#"(w,h)= %0.0f , %0.0f",width,height);
float cr = height/2.0;
profile_InfoSubView_ProfilePic.layer.cornerRadius = cr;
profile_InfoSubView_ProfilePic.layer.masksToBounds = YES;
profile_InfoSubView_ProfilePic.layer.borderWidth = 2.0f;
profile_InfoSubView_ProfilePic.layer.borderColor = [UIColor greenColor].CGColor;
}
Console Output
2015-09-08 10:42:35.156 MyApp[821:60b] (w,h)= 240 , 128
as you can see even after setting image aspect ration to 1:1, its not square
Output On Device (iPhone 4 - iOS 7.1 )

iOS simulator largest image should

I'm having problem in iOS simulator, the image is higher than it should. See the picture.
The correct thing.
Size of images:
#1x = 750x120
#2x = 1500x240
#3x = 2250x360
did not work, I did so:
divAcoes.contentMode = .ScaleAspectFill
divAcoes.backgroundColor = UIColor(patternImage: UIImage(named: "divInfo")!)
You need to provide the proper contentMode for the UIImageView.
That can be done in your Storyboard/XIB with Scale Aspect Fill or in code
imageView.contentMode = .ScaleAspectFill
You can always inspect your interface in runtime by clicking the "Debug View Hierarchy" button in Xcode while running the app.
It's either you have UIImageView's Mode set wrong or its size/position is incorrect.

Resources