I have a button which I am trying to make a perfect circle. I currently have used the code:
startButton.layer.masksToBounds = true
startButton.layer.cornerRadius = startButton.frame.width/2
But as you can see in the photo, the circle drawn in imperfect and cutting off some of the text.
Width and Height are equal.
Related
I wanted a custom slider and the requirement was the height of minView should be 8 pt & maxView should be 4 pt, it's the visible corners should be rounded.
I gave the slider's min & max image as clear image of alpha as 0.0
For maxView, I created a UIView and gave it width equal to slider's width, and made it's corners rouneded.
For minView, I created a UIView and it's leading is equal to leading of slider and trailing should be equal to the center of the thumb.
Here is the logic for what I did.
func changeValue() {
let difference = slider.maximumValue - slider.minimumValue
let thumbWidth = CGFloat(44/2)
let scale = (slider.frame.size.width-thumbWidth)/CGFloat(difference)
let width = scale * CGFloat(slider.value)
minSlideWidth.constant = width
self.view.layoutIfNeeded()
}
But the minView's width doesn't increase and decrease it's width with the thumb's center. If you see the image it had already crossed the UIslider's thumb.
How about this approach ?
Configure properties of UISlide control.
setMinimumTrackImage with 8pt image.
setMaximumTrackImage with 4pt image.
The track images could be re-created by stretchable image in order to keep the rounded corners. You can find it in the section Defining a Stretchable Image section from UIImage reference. It is very useful for track image of slide bar.
Very new to Swift and iOS development. What is the correct way to customise a UIButton?
I am looking to make a UI button that is just a circle, but I want to have the same behaviour as the system buttons: it fades when highlighted, and the fading is animated.
Ideally I would do this without images, and use UIBezierPath, so I can keep a consistent border width for different sizes.
To make a circular button, you can just use the corner radius property! So long as the frame/bounds of the button are a square, if you set the cornerRadius to half of the width, it'll appear as a circle. First, you'll need to import the QuartzCore framework:
import QuartzCore
Corner Radius
To set the corner radius:
myButton.layer.cornerRadius = 8.0
To make a circle given an arbitrary frame, you can use the following:
myButton.layer.cornerRadius = myButton.frame.size.width / 2.0
You may have to set the clipsToBounds property for the corner radius change to be visible:
myButton.clipsToBounds = true
Border
You can also use the layer property to set your border:
myButton.layer.borderWidth = 1.0
myButton.layer.borderColor = UIColor.purpleColor()
(See this post for more examples in Objective-C)
I have a the corner points of a rectangle on the screen and want to fit an UIImageView in this rectangle.
I want it like in this picture:
What's the best way to achieve this?
I think black box is UIImageView and you're trying it to place in screen as in the picture.
You can put the imageview in storyboard, in xib or in code to right side of screen. Then rotate it. For example below code rotates the imageview 20 degress.
float degrees = 20; //the value in degrees
imageView.transform = CGAffineTransformMakeRotation(degrees * M_PI/180);
I have 3 UITextFields with border style none. I want to add borders in code. The effect I want to achieve is to have rounded top corners on first UITextField and to have rounded bottom corners on third text field. Code I am using for rounding edges is here Round top corners of a UIView and add border
But i get this - no right edge and corners are not rounded:
Note: I've set all constraints, that is not a problem. If i use UITextBorderStyleLine right edge is not rounded again.
Please help.
if you want to simplest way to do like on a screen look here>>>
Grey view with clip subviews mode on, and 3 labels/textfields inside, and 2 black view with 1 pixel height
in code..
self.viewCorner.layer.cornerRadius = 6;
self.viewCorner.layer.borderWidth = 1;
self.viewCorner.layer.borderColor = [UIColor blackColor].CGColor;
After you set constraints to grey view and 2 views with 1 pixel height like this
Grey view
1 pixel height view
and result on IPad simulator
Thats all, you can do this for 5 minutes
You need to create custom UItextField or method to change the top and bottom corner to oval shape. Here is a below sample code to top corner similarly you need to do it for bottom left and right corner.
CGRect rect = myTextField.bounds;
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect
byRoundingCorners:UIRectCornerTopLeft |UIRectCornerTopRight
cornerRadii:CGSizeMake(6.0, 6.0)];
CAShapeLayer *layers = [CAShapeLayer layer];
layers.frame = rect;
layers.path = path.CGPath;
myTextField.layer.mask = layers;
How can we make a rounded rectangle frame programatically, as solved with
buyButton.layer.cornerRadius = 2;
buyButton.layer.borderWidth = 1;
buyButton.layer.borderColor = [UIColor blueColor].CGColor;
in iOS 7 round framed button
but resulting in a golden ratio corner such as the one introduced in the iOS 7 icon shape;
?
If you want the same shape, just measure the image, eg with Photoshop and set the border radius proportional to the button's width. I did the measurement for you:
buyButton.layer.borderRadius = 0.315 * buyButton.frame.size.width;
I don't think it follows golden ratio thought. If it does, the ratio should be (1 - (sqrt(5) - 1)/2), which is 0.381..., which results in too rounded corners.
There's more to the golden ratio in this template. It appears in the relative sizes of the circles in relation to each other and the width of the template. See http://www.phimatrix.com/product-design-golden-ratio/ for details.