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.
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.
I wonder why setting the corner radius of a ImageView will results in gray unwanted sharp angle, like this
Noted there is sharp gray angle behind the rounded corner image.
I set up the corner radius like this:
self.previewImageView.image = videoStream.thumbImage;
self.previewImageView.layer.cornerRadius = 8.0;
self.previewImageView.clipsToBounds = YES;
self.previewImageView.layer.masksToBounds = YES;
I also make sure the background color of the image view is white, but it wouldn't help anyway.
Anyone has any idea how to get rid of the sharp gray angle while setting the rounded corner of the image view?
I know I might draw a path and set the layer's mask path, are there any alternative?
My background view is a collection view cell, which happens to be not the same size as the imageView, i can't just set the corner radius of my background view
Try setting the image view's background color to clear.
just do:-
self.yourBackgroundView.layer.cornerRadius=self.yourImageView.layer.cornerRadius
You can try below code --
self.previewImageView.layer.borderColor = (__bridge CGColorRef)([UIColor clearColor]);
Hope this one is helpful.
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;
You will notice the red "+" / "arrows" glyph in the attached screenshot. It is easy enough to change this "origin" point in Xcode. Is there also a way to do this programmatically or is this entirely an Xcode abstraction?
For instance, I want to programmatically create a UILabel and position it by calculating the lower right hand coordinate. In Xcode, I would simply make sure that the red "+" is on the bottom right grid point and define the X, Y, Width and Height parameters with that "origin" in mind.
If you're not using autolayout, you can position a label (or any view) in code by setting its center. So if you know where you want the label's lower right corner to be, you can just subtract half the width and height of the label to compute where its center should be:
CGPoint lowerRight = somePoint;
CGRect frame = label.frame;
label.center = CGPointMake(lowerRight.x - frame.size.width / 2,
lowerRight.y - frame.size.height / 2);
I would recommend just doing that.
But if you want, you can instead go to a lower level. Every view has a Core Animation layer, which is what actually manages the view's on-screen appearance. The layer has an anchorPoint property, which by default is (0.5, 0.5), representing the center of the layer. You can set the anchorPoint to (1, 1) for the lower-right corner:
label.layer.anchorPoint = CGPointMake(1, 1);
Now the label's center actually controls the location of its lower right corner, so you can set it directly:
label.center = somePoint; // actually sets the lower right corner
You'll need to add the QuartzCore framework to your target and import <QuartzCore/QuartzCore.h> to modify the anchorPoint property.
myObject.origin = CGPointMake (0.0,0.0);
I am putting a UIImageView inside a UIScrollView, and trying to control the image so that it is centred on the scrollview after a zoom. and I am not sure the best way to do this.
The apple docs tell us NOT to use the frame property: "Warning If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored." So I am attempting using the following in a UIViewController subclass whose xib contains a scrollView and contained imageView:
scrollView.bounds =
CGRectMake
(scrollView.contentSize.width/2 - scrollView.center.x,
scrollView.contentSize.height/2 - scrollView.center.y,
scrollView.bounds.size.width,
scrollView.bounds.size.height);
containedView.center =
CGPointMake
(containedView.bounds.size.width*scrollView.zoomScale/2,
containedView.bounds.size.height*scrollView.zoomScale/2);
This works accurately where the width and height of the containedView is larger than that of the scrollView and sets the views so that subsequent scrolling will take you exactly to the edges of the containedView. However when either dimension of the image is smaller than the scrollView width and height the image is magnetically attracted to the top left corner of the screen. In the iPad Simulator (only) when the images is shrunk to the size of minimumZoom it does lock on to the centre of the screen. The magnetic attraction is very smooth as if something in the UI is overriding my code after the image has been centred. It looks a bit like a CALayer contentsGravity ( kCAGravityTopLeft ) thing, maybe?
Apple contradict their own advice in their code sample, photoScroller (in a subclass of UIScrollView):
// center the image as it becomes smaller than the size of the screen
CGSize boundsSize = self.bounds.size;
CGRect frameToCenter = imageView.frame;
// center horizontally
if (frameToCenter.size.width < boundsSize.width)
frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
else
frameToCenter.origin.x = 0;
// center vertically
if (frameToCenter.size.height < boundsSize.height)
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
else
frameToCenter.origin.y = 0;
imageView.frame = frameToCenter;
This method does a better job of centring when the image is smaller, but when I try this on my project it introduces some kind of inconsistencies. For example, with scrollView.bounces = NO, a horizontal image whose height is smaller than the height of the scrollView but whose width is larger (so it can be scrolled from left to right) will scroll further to the left than it should (when scrolling to the right it stops correctly at the edge of the image, although if scrollView.bounces = YES it then bounces in from the edge so the image is always cropped on the left) When the image is larger in both dimensions than its containing scrollview this issue accentuates and the whole result feels broken, which is unsurprising given Apple's documented advice.
I have scoured the forums and can't find much comment on this. Am I missing something really obvious?
You don't appear to be using the transform property, so you can ignore that warning about not using the frame property when using the transform property. Go ahead and use the frame property, just like Apple (and the rest of us) do.