Image showing just outside border - iOS/Swift - ios

I'm getting super nitpicky here, because for the most part the border is working here, but the slightest piece of the image still pokes out behind a border I have set. I've tried adjusting the border size and it doesn't help. Is there something wrong with this code? See image below as well for what I'm talking about.
func makeItCircle () {
userImage.layer.masksToBounds = false
userImage.layer.cornerRadius = CGFloat(roundf(Float(self.userImage.frame.size.width/2.0)))
userImage.contentMode = .ScaleAspectFill
userImage.layer.borderColor = UIColor.whiteColor().CGColor
userImage.layer.borderWidth = 3
userImage.clipsToBounds = true
}

It is a known issue with borders. Check my answer to this question if you need a border, otherwise you could just use clearColor.

Its a border width,check by giving border width to just 0.5 or 1.0

Related

Gap between overlaying views in swift

I was wondering how I can get a gap between overlaying views in swift. This is really hard for me to explain so I added a couple images to explain what I'm talking about This is the effect in iMessage The same effect in Facebook Messenger And again in facebook messenger. I am talking about the little gap of space in between the view that says the number of minutes and the profile image (in the 3rd image). I was wondering how I could achieve the same thing in Swift because I'd like to integrate this into my app. Thank you so much!
You can do this many ways.
One of the way, I am posting.
One View, UIView, that should be Square. Inside that, One UIImageView and One SmallView
Constraints as follows:
OuterView: width & height be 120, top 50, and Center Horizontally
ImageView = { 4,4,4,4 },
SmallView = right and bottom as 0, width and height be 40 [Square]
PlusSignImgVw = { 4,4,4,4 }
Like below:
Coding:
override func viewDidAppear(_ animated: Bool) {
imageVw.layer.cornerRadius = imageVw.frame.size.width / 2
smallSquareView.layer.cornerRadius = smallSquareView.frame.size.width / 2
smallSquareView.clipsToBounds = true
plusSign.layer.cornerRadius = plusSign.frame.size.width / 2
plusSign.clipsToBounds = true
}
Output:
It's a bit tricky, not obvious at first, but not too difficult either.
For the round images, try this:
customView.layer.cornerRadius = customView.bounds.size.width / 2.0
customView.layer.borderWidth = 2.0
customView.layer.borderColor = UIColor.white.cgColor
For the rectangular image with round corners, first line will calculate the .cornerRadius based on height (or you can actually do it for all the cases, and it would also work):
customView.layer.cornerRadius = customView.bounds.size.width / 2.0
Just replace customView with yourCustomViewName and write those 3 lines for each view. That should do the job.

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.

why edges of textfield Get Clipped?

Edges of UITextfield get Clipped form all the corners when i set corner radious property.
does anybody knows why this happening?
thanks!!!
my Code:
override func viewDidLoad() {
super.viewDidLoad()
fullNameTextField.layer.cornerRadius = fullNameTextField.frame.size.height / 2
}
result:
i want to achieve this:
Try setting your border style to none and draw your border using in layer:
fullNameTextField.borderStyle = .none
fullNameTextField.layer.borderColor = UIColor.gray.cgColor
fullNameTextField.layer.borderWidth = 1.0
fullNameTextField.layer.cornerRadius = fullNameTextField.frame.size.height / 2
yes buddy its because your text field contains boder style. in interface builder select your text field and choose rounded boder style if you need border.. attaching an image have a look and you can fix it from storyboard :)

ios swift 3 xcode8 beta rounded imageView

With my project I use the following code in order to make my images in rounded shape:
profileImage.layer.cornerRadius = profileImage.frame.size.width / 2
profileImage.clipsToBounds = true
I also use contrains for my image to make it width = hight, and other constrains.
After ugrading my project to xcode 8 beta, and swift 3. All the images views that I set to rounded were disappeared, and when I remove the code for making it rounded or I remove all the constrains they appear again.
But I still need them to be rounded. Anyone can help me to fix the issue.
Thanks
I had the same problem, the solution was just to move a line of code before your layer modifications.
Try to apply layout changes:
self.view.layoutIfNeeded()
before your code:
profileImage.layer.cornerRadius = profileImage.frame.size.width/2
profileImage.clipsToBounds = true
OR
place your code related to frames / layers into viewDidLayoutSubviews() method:
override func viewDidLayoutSubviews() {
profileImage.layer.cornerRadius = profileImage.frame.size.width/2
profileImage.clipsToBounds = true
}

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.

Resources