Make UISwitch border width smaller - ios

Hello I am trying to make UISwitch borderWidth smaller, but it doesn't look correct.
All I did is
layer.borderWidth = 0.5

instead of using storyboard try to customise switch programmatically . try using following code as if you change one parameter must adjust radius of switch too. Hope it helps
OnOffSwitch.layer.borderWidth = 1.0;
OnOffSwitch.layer.cornerRadius = 16.0;
OnOffSwitch.layer.borderWidth = 0.5;
OnOffSwitch.layer.cornerRadius = 17.0;

Related

Xcode 8 - Some buttons border removed

I just updated my Xcode ver from 7.3 to 8.0 and some buttons borders disappeared.
The code looks fine, so I really don't know what happened to the layers.
btw - in some other controllers I can see the layers borders.
self.button.layer.borderColor = borderColor.CGColor;
self.button.layer.borderWidth = 2;
self.button.layer.cornerRadius = CGRectGetHeight(self.button.frame) / 2;
before: (The image is only for example - the borders looks different at real time)
now:
The reason is that XCode 8 introduces a new way to zoom in Storyboards.
Before XCode 8, in the view controller life cycle, frames were not known in viewDidLoad (or in properties didSet). You had to wait until viewDidLayoutSubviews (which is when Autolayout had finished applying the constraints to determine the frames of every subview in the main view.
But bounds were accessible before that: they were just set to the size of the IBOutlet in the storyboard.
In XCode 8, things are different : due to their new zooming system, even the boundsare not correct before ViewDidLayoutSubviews (they may exist but with dummy values like 1000 x 1000).
In conclusion :
you can use such things as cornerRadius in viewDidLoad or in the IBOutlet
didSet, as long as you use a fixed value
if you need to define your cornerRadius based on bounds, then do so in viewDidLayoutSubviews, or use NSLayoutConstraints (their value is fixed and known from Autolayout)
if you need to use cornerRadius in views (like UITableViewCell or UICollectionViewCell subclasses), then you can either do so in layoutSubviews (but then you need to give either a fixed value or a NSLayoutConstraint constant to cornerRadius), or in awakeFromNib(in that case, just add self.layoutIfNeeded before doing anything frame- or boounds-related, in order to force the cell to recalculate its subviews' frame).
I think problem in it:
CGRectGetHeight(self.button.frame) / 2;
When you set corner i think height button don't have value or value to larger border will don't show. You can try change it to
self.button.layer.cornerRadius = 15;
If work, I think you can check your logic and set it when height button get right value.
Try this and check:
#import <QuartzCore/QuartzCore.h>
self.button.layer.borderColor = [UIColor whiteColor].CGColor;
self.button.layer.borderWidth = 2;
self.button.layer.cornerRadius = 5; // Change this value on your requirement
self.button.clipsToBounds = YES;
To force view frame to be calculated, you can try with layoutIfNeeded.
For example for a label in a UITableViewCell :
override func awakeFromNib() {
super.awakeFromNib()
self.layoutIfNeeded()
self.qualityIndexLabel.makeRound()
}
use dispatch_after
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.button.layer.borderColor = borderColor.CGColor;
self.button.layer.borderWidth = 2;
self.button.layer.cornerRadius = CGRectGetHeight(self.button.frame) / 2; });
the RoundedRect is deprecated use UIButtonTypeSystem instead.
see https://developer.apple.com/reference/uikit/uibutton?language=objc for more information.

iOS: Image is not displaying properly in UIImageView

I have a UIImageView in which a UIImage is showing which comes from JSON. The problem is that I want to set the cornerRadius for the UIImageView. I put the following code for it:
imgVw.layer.cornerRadius = 30.0;
imgVw.layer.borderWidth = 2.0;
imgVw.layer.borderColor = [UIColor blackColor].CGColor;
It displays the cornerRadius and border for UIImageView but not in the proper way. The image is showing outside the corners like in screenshot.
You need to set the masksToBounds property of layer to YES like this
self.imgVw.layer.masksToBounds = YES;
Set the clipsToBounds property of UIImageView to YES.
Like
set imgVw.clipsToBounds=YES;

How to get a circle image in UIImageView

I tried to get a circle image using layer, here is my code
_compassArrow.layer.cornerRadius = _compassArrow.frame.size.width / 2;
_compassArrow.layer.masksToBounds = YES;
_compassArrow.layer.borderColor = [UIColor whiteColor].CGColor
the compassArrow is an imageview which display the compass image. And when I run my program, it looks terrible:
my actual picture
I don't know what happened to it. I've add some constraints to it, to make it has equal width with the device. Does this influence my image?
I think you set cornerRadius before your constraints are applied. Try to put this code in layoutSubviews or viewDidLayoutSubviews for example.
This way, the _compassArrow.frame.size.width value will be the one after constraints applied on it, and you'll get the correct cornerRadius.
Here is a piece of code that should allow you to do this.
_compassArrow.layer.borderWidth = 1.0
_compassArrow.layer.masksToBounds = false
_compassArrow.layer.borderColor = UIColor.whiteColor().CGColor
_compassArrow.layer.cornerRadius = profilePicture.frame.size.width/2
_compassArrow.clipsToBounds = true

Custom Rounded Button without Corner Radius

how do we subclass a UIbutton and customize it so that it has a round end like this one on Periscope. The Following Button.
I would like to PREVENT using layer.CornerRadius though.
Using a BezierPath perhaps? but I have a math problem creating the Path.
Any thoughts? Thank you
I have created that button using the layer.CornerRadius as below :-
self.buyButton.layer.cornerRadius = self.buyButton.frame.size.height/2;
self.buyButton.clipsToBounds = YES;
[UPDATED CODE - FOR SHADOW]
self.buyButton.layer.masksToBounds = NO;
self.buyButton.layer.borderWidth = 1.0f; //Just for look n feel
self.buyButton.layer.shadowColor = [UIColor greenColor].CGColor;
self.buyButton.layer.shadowOpacity = 0.8;
self.buyButton.layer.shadowRadius = 12;
self.buyButton.layer.shadowOffset = CGSizeMake(12.0f, 12.0f);
But why u don't want to use the method already provided as per your custom requirement I don't think you need to use bezier path for this.
UPDATED SCREENSHOT :

Change border width of label in custom UICollectionCell

I have custom UICollectionViewCell and I have placed 2 UILabels in it. Now I want to change the borderRadius and borderWidth of the labels in it. Can anyone tell me how to do this?
I am doing in this way, but nothing is happening:
self.titleLabel.layer.borderWidth = 2.0;
self.titleLabel.layer.borderColor = [UIColor blackColor].CGColor;
I am putting these two lines in initWithFrame method.
If you want to set the borderWidth of UILabel inside means why you are using
self.titleLabel.layer.borderWidth = 2.0;
Instead of this you can directly use the label name like,
yourLabel1.layer.borderWidth = 2.0;
yourLabel2.layer.borderWidth = 2.0;
Like wise you can change the borderRadius also.
With the following line added the border should appear:
self.titleLabel.layer.masksToBounds = YES;

Resources