How to make UIButton Exactly round - ios

I want to make UIButton exactly round,this code is creating oval button
_img.layer.borderWidth=2.0;
_img.layer.cornerRadius = 100;
_img.layer.masksToBounds = YES;
_img.layer.borderColor=[UIColor blackColor].CGColor;
_img.layer.borderWidth=.5f;
Please help

the property cornerRadius should be the half of the width / height (height and width should be the same)
use this code for a button exactly round:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(aMethod) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(135.0, 70.0, 40.0, 40.0); //width and height should be same value
button.clipsToBounds = YES;
button.layer.cornerRadius = 20; //half of the width
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.borderWidth= 2.0f;
[self.view addSubview:button];

First of all, why did you assign border width twice? Though it has nothing to do with round button but it's a bad coding example. Anyway, make sure that _img width and height are both equal to 200 if you want a rounded button with 100 corner radius. e.g.
_img.frame = CGRectMake(/* x origin of the button*/, /*y origin of the button*/, 200, 200);
_img.layer.cornerRadius = 100;
_img.layer.clipsToBounds = YES;
_img.layer.borderColor=[UIColor blackColor].CGColor;
_img.layer.borderWidth=.5f;// or 2.0f
hope this will make your button, or whatever _img is, rounded.

First make img width and height same
Img.clipstobounds=YES;
And then make layer.cornertadius equal to width/2 of img

Related

how to change UI Button width and height dynamically with respect to image in ios

How to change UIButton width and height dynamically with respect to image in ios?
[[btn_dynamic imageView] setContentMode: UIViewContentModeScaleToFill];
// btn_dynamic.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
// btn_dynamic.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
//[btn_dynamic setContentMode:UIViewContentModeScaleAspectFit];
I tried in these area but not working
//Get the size of image
UIImage *buttonImage = [UIImage imageNamed:#"btnImage"];
CGSize buttonSize = CGSizeMake(buttonImage.size.width, buttonImage.size.height);
buttonInstance = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonInstance setImage:buttonImage forState:UIControlStateNormal];
//Set the frame of button using image size
[buttonInstance setFrame:CGRectMake(0, 0, buttonSize.width, buttonSize.height)];
Try this code
float x=0;
for(int i = 100; i < 110; i++)
{
btn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
UIImage * img = [UIImage imageNamed:#"Round_Rect4.png"];
[btn setBackgroundImage:img forState:UIControlStateSelected];
titleString = [titleArray objectAtIndex:i-100]; // get button title
CGSize fontSize = [titleString sizeWithFont:[UIFont systemFontOfSize:12.0]];
CGRect currentFrame = btn.frame;
CGRect buttonFrame = CGRectMake(x, currentFrame.origin.y, fontSize.width + 22.0, fontSize.height + 12.0);
[btn setFrame:buttonFrame];
x = x + fontSize.width + 35.0;
[btn setTitle:titleString forState: UIControlStateNormal];
[btn setTag:i];
[self.view addSubview:btn];
}
You can use the following to change the button size.
CGSize imageSize = image.size;
CGRect rectButton = button.frame;
rectButton.size = imageSize
button.frame = rectButton
You need to create custom button as below
UIButton *btnDynamic = [UIButton buttonWithType:UIButtonTypeCustom];
[btnDynamic setImage:[UIImage imageNamed:#"image_Name"] forState:UIControlStateNormal];
It simply create a custom button as per image..
Hope it helps...!
If you do not want to touch button frame and want to set image that do not look stretchy. Go with the code below
btn_dynamic.imageView.contentMode = UIViewContentModeScaleAspectFit;
If you want to set button width and height, you need to set the button size according to the image size.
UIImage *image = [UIImage imageNamed:#"image.jpg"];
CGRect btnFrame = btn_dynamic.frame;
btnFrame.size.width = image.size.width;
btnFrame.size.height = image.size.height;
[btn_dynamic setFrame:btnFrame];
If you want to maintain the ratio as the image could be larger than the view
UIImage *image = [UIImage imageNamed:#"image.jpg"];
CGRect btnFrame = btn_dynamic.frame;
btnFrame.size.width = 100; // lets the button width is 100 or set according to the need.
btnFrame.size.height = (btnFrame.size.width*image.size.height)/image.size.width;
[btn_dynamic setFrame:btnFrame];
Try this simple solutions
TestButton.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 8);
[TestButton sizeToFit];
float width = TestButton.frame.size.width + 20;
TestButton.frame = CGRectMake(TestButton.frame.origin.x,
TestButton.frame.origin.y, width, 40);
who ever looking for an auto layout solution what i did was i kept a temp uilable with text color clear color, at bottom of the uibutton and set the auto layouts as,
1. uibutton left = tmp uilabel left
2. uibutton right = tmp uilabel right
so by setting tmp label text, the label will auto adjust its width according to the text and since the label and button have equal left and right the button will also automatically expand its width.
Hope this helps..

Get dynamic Coordinates of created subview ios

I created a method that creates a subview based on the input size so there is not a defined and default corner CGRect values.I want to create a dismiss button that will be located at the top left corner of each subview. Currently I'm using this method to add it, but the problem with this is that the values for CGRectMake can not be static.
CGRect tapToDismissFrame = CGRectMake(50.0f, 50.0f, 400.0f, 400.0f);
UIButton *tapToDismiss = [[UIButton alloc] initWithFrame:tapToDismissFrame];
tapToDismiss.backgroundColor = [UIColor clearColor];
tapToDismiss.titleLabel.font = [UIFont fontWithName:[NSString stringWithFormat:#"Helvetica-Bold"] size:20];
[tapToDismiss setTitle:#"⊗" forState:UIControlStateNormal];
[tapToDismiss setTitleColor:[UIColor colorWithRed:173.0/255.0 green:36.0/255.0 blue:36.0/255.0 alpha:1.0] forState:UIControlStateNormal];
[tapToDismiss addTarget:self action:#selector(tapToDismissClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.maskView addSubview:tapToDismiss];
How can i get the dynamic values in order to create it in the top left or right corner.
You should use CGGeometry functions to calculate correct frame for your dismiss button. Suppose you created that button. After that point here is a sample code:
[button sizeToFit];
CGRect maskViewBounds = self.maskView.bounds;
CGFloat top = CGRectGetMinY(maskViewBounds);
CGRect left = CGRectGetMaxX(maskViewBounds);
CGFloat buttonHeight = button.size.height;
CGFloat buttonWidth = button.size.width;
CGFloat topMargin = 5.0f;
CGFloat leftMargin = 5.0f;
CGPoint buttonCenter = CGPointMake(top + buttonHeight / 2.0 + topMargin, left - buttonWidth / 2.0 - leftMargin);
button.center = buttonCenter;
Note: I didn't write this code in Xcode. So there may be type errors.

Add buttons to UIView

I would like to add buttons to an UIView. The number of buttons varies from view to view.
I want to achieve the following:
I tried to add the Buttons (in a foreach construct) like this:
UIButton *but=[UIButton buttonWithType:UIButtonTypeRoundedRect];
but.frame= CGRectMake(200, 15, 15, 15);
[but setTitle:#"Ok" forState:UIControlStateNormal];
[but addTarget:self action:#selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:but];
But I don't know how to calculate the CGRectMake when it comes to an new line. How can I check the end of the line (UIView)? Or is it kinda stupid to add Buttons like this?
Thanks
Florian
You need to add calculate buttons size after you set the title. Then keep track of the current X and Y of the previous buttons and figure out if the newly created button can fit on the current "line". Otherwise go to the next one.
I suggest you to look at existing components and see how they do it, below are a couple links.
https://github.com/domness/DWTagList
https://github.com/andreamazz/AMTagListView
With the following code , you can place multiple buttons in one row as long as their total width and the gap is smaller then the view's width.
UIView *view = nil;
NSArray *allBtns = nil;
float viewWidth;
float currentY = 0;
float currentX = 0;
float btnGapWidth = 10.0;
float btnGapHeight = 2.0;
for (UIButton *btn in allBtns) {
CGRect rc = btn.frame;
BOOL shouldAddToNewLine = viewWidth - currentX - btnGapWidth < rc.size.width ? YES : NO;
if (shouldAddToNewLine) {
rc.origin = CGPointMake(0, currentY + rc.size.height + btnGapHeight);
currentX = 0;
currentY += rc.size.height + btnGapHeight;
} else {
//another row
rc.origin = CGPointMake(currentX + rc.size.width + btnGapWidth, currentY);
currentX += rc.size.width + btnGapWidth;
}
btn.frame = rc;
[view addSubview:btn];
}

Labels for UISlider values and centering text

I have a slider and would like to display text labels for some of the values on the slider.
I would like numbers to be centered on top of values. Labels are of fixed width - predefined. So I think all I need to do is calculate space width in between labels and keep incrementing x position when positioning each label. The problem that I am seeing though is there more labels there are the more each label slides to the right. I know this because the value set is always in the center of the slider in my test cases. Text is centered within label. Please see pictures on the first picture labels are almost correctly centered. The way to see what I mean is looking at the last label in both pictures. I almost think that it is the border of the label perhaps but I do not set the border either. I am not sure what I could be doing wrong in terms of math, but must be missing something. Also, it seems like the fir label maybe more than 1/2 label width of to the left of slider start position.
EDIT This is because the frame X starting position DOES not match the x starting position of the slider. See pic. Now I just need to figure out how to calculate the offset. The red line is drawn at slider.frame.origin.x. You can see that slider is off by a few points. The ball should be in the center at 3.125 - 25 .. but it is slightly of.
The way space between labels is calculated:
float xPosition = sliderFrame.origin.x;
//Slider adjusted width
float sliderWidth = sliderFrame.size.width - xPosition;
float spacedApart = (sliderWidth - (LABEL_WIDTH * numberofticks)) / (float) (numberofticks);
Each label is displayed at:
CGRect mpkLabelFrame = CGRectMake(xPosition - LABEL_WIDTH/2, (yPosition - spaceBetweenLabelsAndSlider), LABEL_WIDTH, LABEL_HEIGHT)+3; //need to increase by 3 to make it work better
//display label do other things
xPosition += (LABEL_WIDTH + spacedApart);
You don't need to take into account the sliders x coordinate to figure out the spacing. In fact, since they are distributed equally you don't need the label size as well.
CGFloat labelSpacing = sliderFrame.size.width/(numberOfTicks-1);
xPosition = sliderFrame.origin.x;
Set the label's frames:
CGRect mpkLabelFrame = CGRectMake(xPosition - LABEL_WIDTH/2, (yPosition - spaceBetweenLabelsAndSlider), LABEL_WIDTH, LABEL_HEIGHT);
xPosition += labelSpacing;
EDIT: code for testing the approach in an viewController:
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(40, 100, 240, 10)];
[self.view addSubview:slider];
int numberOfTicks = 7;
CGFloat labelSpacing = slider.frame.size.width/(numberOfTicks-1);
CGFloat xPosition = slider.frame.origin.x;
CGFloat labelWidth = 10;
CGFloat yPosition = CGRectGetMinY(slider.frame)-20;
for (int i=0;i<numberOfTicks;i++){
CGRect labelFrame = CGRectMake(xPosition - labelWidth/2, (yPosition), labelWidth, 20);
UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
label.backgroundColor = [UIColor redColor];
label.adjustsFontSizeToFitWidth = YES;
label.text = [NSString stringWithFormat:#"%i",i];
[self.view addSubview:label];
xPosition += labelSpacing;
}
}

iOS: adding images to a UIButton, then putting button in a scrollview, but only seeing one image?

I'm working on an iPad app and part of the UI is a scrollview that has buttons for its contents. I'm working on adding images to these buttons, but when I do so, I only ever get the image in one spot, when I should be seeing it on all the buttons. This is what I'm doing:
float scrollCurrTop = 0;
CGRect currProcedureButtonFrame = CGRectMake(0,
scrollCurrTop,
self.fProceduresView.frame.size.width,
self.fLabelAndButtonHeight);
PatientIDButton* currProcedureButton = [PatientIDButton buttonWithType:UIButtonTypeCustom];
[currProcedureButton setFrame:currProcedureButtonFrame];
[currProcedureButton.layer setBorderColor: [self.fPanelViewBorderColor CGColor]];
[currProcedureButton.layer setBorderWidth: self.fBorderWidth];
currProcedureButton.titleLabel.font = self.fLabelFont;
NSString* displayName = [grabbing name];
if (displayName == nil)
{
displayName = currPlanName;
}
[currProcedureButton setTitle:displayName
forState:UIControlStateNormal];
[currProcedureButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
currProcedureButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
currProcedureButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
// is the plan approved?
if ([self isPlanApproved:currPlanName])
{
// add the checkmark to this plan button
CGRect currPlanButtonFrame = currProcedureButton.frame;
float originX = currPlanButtonFrame.size.width - (currPlanButtonFrame.size.width/3.0f);
float originY = currPlanButtonFrame.origin.y;
float width = currPlanButtonFrame.size.width - originX;
float height = currPlanButtonFrame.size.height;
CGRect currPlanApprovalImageFrame = CGRectMake(originX, originY, width, height);
UIImageView* currPlanApprovalImage = [[UIImageView alloc] initWithFrame:currPlanApprovalImageFrame];
[currPlanApprovalImage setBackgroundColor:[UIColor colorWithRed:(63.0f/255.0f)
green:(179.0f/255.0f)
blue:(79.0f/255.0f)
alpha:1.0f]];
[currPlanApprovalImage setImage:self.fCheckMarkIcon];
[currProcedureButton addSubview:currPlanApprovalImage];
}
[self.fProceduresView addSubview:currProcedureButton];
scrollCurrTop += self.fLabelAndButtonHeight;
Where 'fProceduresView' is the scrollview that houses the buttons. What am I doing wrong?
It seems that you're misunderstanding the logic behing setting the frame of the imageview's those you're trying to add
CGRect currPlanButtonFrame = currProcedureButton.frame;
float originX = currPlanButtonFrame.size.width - (currPlanButtonFrame.size.width/3.0f);
float originY = currPlanButtonFrame.origin.y;
float width = currPlanButtonFrame.size.width - originX;
float height = currPlanButtonFrame.size.height;
CGRect currPlanApprovalImageFrame = CGRectMake(originX, originY, width, height);
UIImageView* currPlanApprovalImage = [[UIImageView alloc] initWithFrame:currPlanApprovalImageFrame];
You don't need to set originY to currPlanButtonFrame.origin.y;
All subviews have relative coordinates to their's superviews.
So in your case originY should be 0
For example:
// Image is visible
Button frame [0, 0, 100, 100]
image in button [0, 0, 100, 100]
// Button is visible, but image is not because it will be clipped by bounds of the button.
Button frame [100, 100, 100, 100]
image in button [100, 100, 100, 100]
Also you will be able to "see" your images, if you set
currProcedureButton.clipsToBounds = NO

Resources