why edges of textfield Get Clipped? - ios

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 :)

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
}()

change popover viewcontroller cornerRadius in swift

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

Swift circular corners doesn't work properly on different screen sizes

I'm trying to achieve smooth round corners on imageviews. Although it's working well on non-plus devices, i can not reach my goal at plus screen devices. Incidentally, i recognize, it's not even work well on the devices that font size enlarged. I applied following code that can be found every topic.
mergedImage.image = lastImage
mergedImage.layer.masksToBounds = false
mergedImage.layer.cornerRadius = mergedImage.frame.size.height / 2
mergedImage.clipsToBounds = true
And it's results like pictures below.
It looks like the black shape changes size. This can happen when using AutoLayout for example. If that is the case, you need to calculate the corner radius each time the frame changes.
I think the best way to do this is by subclassing UIView to create a "black shape view", and then overriding its layoutSubviews method:
override func layoutSubviews() {
super.layoutSubviews()
layer.cornerRadius = bounds.height / 2
}
If you don't have a subclass, you can for example do this in UIViewController.viewDidLayoutSubviews:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
mergedImage.layer.cornerRadius = bounds.height / 2
}

Image showing just outside border - iOS/Swift

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

Bordered iOS 8 Buttons Swift

I want a flat looking white bordered around my UIButton. I would like it in Storyboard or programmatically. My code isn't working.
Code:
UIButton.layer.cornerRadius = 2;
UIButton.layer.borderWidth = 1;
UIButton.layer.borderColor = UIColor.whiteColor()
You should create referencing outlet for your button from storyboard to your VC named for example myButton than set its properties
myButton.layer.cornerRadius = 2;
myButton.layer.borderWidth = 1;
myButton.layer.borderColor = UIColor.whiteColor().CGColor
You don't have to do this with code either. You can create a stretchable image and set it to the background image of the button in the attributes inspector.
as 0x7fffffff said. UIButton is the class it can be instatiated bu invoking its constructor like this
let instanceOfUIButton = UIButton()
then you can set the desired attributes:
instanceOfUIButton.layer.cornerRadius = 2;
In Xcode 8.2 (Swift 3) you can use "Identity Inspector Tab". Search for "Users Defined Runtime Attributes", after selecting your UIButton. There you can define these attributes:
Key Path: layer.cornerRadius; 2-Type: Number, 3-Value: 2
Key Path: layer.borderWidth; 2-Type: Number, 3-Value: 1
Key Path: layer.borderColor; 2-Type: Color, 3-Value: "select white or another one"
Another option instead of creating a reference to each button would be to create a subclass of the type UIButton. You could then set the properties in the subclass. Next you could change the class of all the buttons in the storyboard that need to have the same properties.
class MyButton: UIButton {
override func draw(_ rect: CGRect) {
super.draw(rect)
layer.borderWidth = 1.0
layer.borderColor = UIColor.White
layer.cornerRadius = 2
}
}
Add this line at the top
myButton.layer.masksToBounds = true

Resources