Using .roundedRect vs. cornerRadius of the layer for rounded corners - swift

I was experimenting with creating a simple textfield in code and have been using UITextField.layer.cornerRadius to create a rounded corner, rather than using .roundedRect property of borderStyle, which I thought looked more restrictive.
So I just came back to wondering about it, and would like to know if there is any advantage to using .roundedRect?
It seems to display a default standard roundedness of the corners - can this be adjusted, or is it just there to be available off the shelf?

You can programmatically tune the border width and corner radius of the text field and any other view for the matter, by accessing its layer properties:
UITextField.layer.cornerRadius = 5.0
UITextField.layer.borderWidth = 3.0
On top of that, UITextField has a borderStyle property which you might want to play with. It has four possible values: None, Line, Bezel, and RoundedRect.
more check roundedRect apple doc
Displays a rounded-style border for the text field.
Advantage is if you are using .roundedRect it will give standard rounded-style & border Width whereas if you use .cornerRadius you can tune programatically the border width and corner radius.

Related

Issue with UIProgressView not running smoothly once height is increased and corners are rounded

I'm experiencing an issue with a UIProgressView (using Xcode 11.5 and current Swift version). My aim is to create a progress bar with an increased height (vs. the Apple standard height) and rounded corners, which represents the progress of a played audio file smoothly.
intended progress bar style
I can easily achieve the desired look by combining a height constraint in interface builder with the following code:
audioProgress.layer.cornerRadius = bubble.frame.size.height / 3
audioProgress.clipsToBounds = true
However, the issue I'm experiencing is that the progress bar does not start smoothy from the beginning – instead it jumps to about 1/3 of the bar (changes depending on device size) and stops there, until the audio "catches up" on that position, from where it continues running smoothly until the end.
I made sure this is not an error in my code re audiofiles, timer etc. but some layout issue. E.g. once I change the height of the bar back to Apple's standard height, the progress shows as it should.
I can avoid this odd behaviour by using the below code to increase the bar's height (instead of a constraint):
let transform: CGAffineTransform = CGAffineTransform(scaleX: 1.0, y: transformFactor)
audioProgress.transform = transform
However, this no longer allows me to round the corners as shown above, as the combination of layer.cornerRadius and the CGAffineTransform commands leads to oddly skewed corners, as shown in other posts.
So my 2 questions are:
Can anyone explain what causes this weird behavior of the bar jumping and stopping for a while in the first place? And how to avoid it?
If using the CGAffineTransform command is the only way to go – is there some other way to achieve the rounded corners? E.g. sth along the lines of "first transform the height, and only THEN round the corners" (as opposed to doing it the other way around, which, in my understanding, causes the skewed look...)
Try this -
audioProgress?.layer.cornerRadius = bubble.frame.size.height / 3
audioProgress?.layer.masksToBounds = true

iOS Xcode storyboard: How do I make text look like this design

I have built this app from our designer, and we are finishing it up but I am not sure how to adjust this image in xcode storyboard of the On and Off text to look like the one in the design. I tried changing the shadow, but that just makes it darker. I am just using a standard label and it just comes in black.
Wire up your label to your code as an outlet in the usual way and then add the following to each:
OFF_Button.shadowColor = UIColor(red: 0, green: 60, blue: 120, alpha: 0.3)
OFF_Button.shadowOffset = CGSizeMake(1.5,1.5)
Where OFF_Button is the name of your label.
That will get you this:
You can tweak the offset, RGB values and alpha to get the exact style that your designer is looking for.
Obviously, if you've got multiple labels in your project that all want to have the same appearance then it makes sense to create a label subclass with this appearance built in.
Alternatively, if you want to do it all in storyboard then you can make a duplicate label, same text, different colour and then align it with the original label, but add a small value (probably between 1 and 2) to the 'constant' value in the alignment boxes under the size inspector. Moving this offset label to the back will give you the same effect. As is usually the case, the programatic route seems tidier.

Add shadow to a UITabbar in iOS8

I wanna add a simple shadow to my UITabbar.
I added a shadow image (a 10x1 gradient) to my project / related storyboard property.
The image should be repeated by itself, shouldn't it?
Nevertheless, there is no shadow in the design mode as well no shadow if I launch the application.
Would could be wrong?
This can be done programatically also:
self.tabBarController?.tabBar.layer.masksToBounds = false
self.tabBarController?.tabBar.layer.shadowColor = CustomColors.Graphites.dark.cgColor
self.tabBarController?.tabBar.layer.shadowOpacity = 0.8
self.tabBarController?.tabBar.layer.shadowOffset = CGSize(width: 0, height: -4.0)
self.tabBarController?.tabBar.layer.shadowRadius = 3
keep in mind:
read
understand
To use an image shadow you have to set a background image.
I used a simple transparent / bg-colored 16x16 image.
After that, the the shadow appeared.

Composite Chart + Objective C

I want to implement below chart like structure.
Explanation:
1. Each block should be clickable.
2. If the block is selected, it will be highlighted(i.e. Red block in figure).
I initially google for this but was unable to find. What should be "Drawing logic" corresponding to this with animation?Thanx in advance.
I think you need to use MCSegmentedControl.
You can get it from here.
Generally speaking, I'd have an image for the outline with a transparent middle, then dynamically create colored blocks behind it of the appropriate colors, with dynamic labels. The highlighting is a little tricky, but could be done with a set of image overlays. One could also try to shrink and expand fixed images for the bars/highlighting, but iPhone scales images poorly.
(Will it always be 4 blocks? There are a couple of other ways to manage it using fixed-size images overlaying each other.)
Maybe you should look into using CALayer for this?
U need to implement this type of logic using button. Just scale button width according to percentage.
And to make round rect button like appearance use the code below and don't forget to import quartz-core framework in class file.
And to scale first and last button as you need some overlap from nearby button.
btn.layer.cornerRadius = 8.0;
btn.layer.borderWidth = 0.5;
btn.layer.borderColor = [[UIColor blackColor] CGColor];

Anchor UIButton to the left of a UILabel

I have a UILabel with right-aligned text that may vary from between three to seven characters in length. I am trying to anchor a button to the left of this text so that when the length of the text increases, the button will stay the same distance from the left of the label.
Is there an anchor setting in Interface Builder, or does it require a code-level solution (e.g. setting the button's x position according to the width of the label)?
You need to do this in code. Interface builder is not build for runtime calculations.
To calculate the width of your text you can use NSString's sizeWithFont: method.