Change Button Width and Height Programmatically Xcocde - frame

I have an app that you tap a button and at certain points you get time added. I want to add to the app that whenever the certain point is met, the button height and width get smaller. How would I go about doing that?

If your button is named, say, button, this will make it 5 pixels smaller in all directions:
[button setFrame:CGRectInset(button.frame, 5, 5);
You can have more freedom by using any CGRect value you wish; but setFrame is the way to go.

Related

UIButton width doesn't increase based on text if insets are given

I have given trailing, top and height constraints to the button.
As you can see in the attached screenshot, if I remove all the insets given, then button looks perfect and button fits based on text length.
But I want to give right-left title insets as well as left image inset.
Then button is not displaying text properly as shown in the attached screenshot.
Auto-layout can be a little tricky when adjusting the Title Label of a UIButton.
From Apple's docs for titleEdgeInsets:
This property is used only for positioning the title during layout. The button does not use this property to determine intrinsicContentSize and sizeThatFits(_:).
You have a couple options...
One is to subclass UIButton and override .intrinsicContentSize.
Another option, which may be better for your case, is to adjust the Content Insets instead of the Title Insets.
As you described, this button has Top / Trailing / Height constraints:
Note that the Left value for the Image Inset is a negative value. You may / probably will want to tweak the values I used.

Two UIbutton with different width after autoresizing

I have two UI buttons placed equal horrizontally, i have tried many autolayouts, but i could not make it both of equal heights and within screen limits. i can make it of same width but then the second button goes out of screen. Screen shot is attached as well.
Can you please guide me how can i do this?

How to create a small button with a large tappable area

I want to create a close button which will look like a circle with an x in the middle. In x code I set the button's size and width to be large so that the touchable area is larger (50 x 50 with a font of just 22).
I create a button, change the title to X and then set the following:
[self.closeButton.layer setBorderWidth:2.0f];
[self.closeButton.layer setBorderColor:[[UIColor whiteColor] CGColor]];
[self.closeButton.layer setCornerRadius:self.closeButton.bounds.size.width/2];
The circle border is too far from the X. How would I bring the borders in tighter to the X but not decrease the size of the clickable area?
assign image to a button. and make tappable area as you want. then set image location in button by setting inset value of that button as top, bottom, left, right. change the value of this according to your requirements. You can set inset value from interface builder as shown in image here. change the value and see the difference to place your image in button at exact location you want.
exactly like this image
I'd suggest exactly what Max has done. He has shown how to do this via IB, this is how you do it through code:
myButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 25, 25);// UIButton
myBarButtonItem.imageInsets = UIEdgeInsetsMake(0, 0, 25, 25);// UIBarButtonItem
EDIT:
Sorry for not reading your question properly the first time. I think you will run into the same issue with this approach : the border/cornerRadius of the button will not respect any insets. It will be drawn according to the frame of the button. Only (easy) workaround at the top of my head, is to create an image with the border and corners in it, and then set it as the image. The image will respect the insets, and you will have your desired tappable area, with the borders exactly where you want them.
There might be a more elegant workaround through subclassing, but unless you change the border width/color, or corner radius of your button at any stage, I'd suggest sticking with custom image.
EDIT 2:
Instead of using an image you might want to use this unicode character.

Xcode cant click button after moving button frame

I created a button in the Interface Builder with a tag 10
In code
UIButton *btn = (UIButton*)[self.view viewWithTag:10];
btn.frame = CGRectMake(btn.frame.origin.x,btn.frame.origin.y+changeInPos, btn.frame.size.width, btn.frame.size.height);
changeInPos could be any number lets say its 100. So my button moves down 100 but it no longer works in its new position nor does it work if I click where it used to be.
Have I done something wrong after moving the button? If its useful to know I have turned off use autolayout.
One possible explanation is that the button moves out of the frame of its containing view. In this case, it will not receive touches.

scrollbar contentsize change when keyboard appears

I have a view controller with a scrollview with lots of text boxes, when i click on one of the text boxes and the keyboard appears, the content size of the scrollview isn't long enough to show everything with the keyboard open. How can i change the content size every time the keyboard pops up and set it back once it vanishes?
Also is there a method to make it vanish without resignfirstresponder on every textfield?
Hope someone can help me, I'm pretty new to Objective C.
contentSize probably isn't the property you should be changing.
Try changing the frame property of the scroll view (i.e. make it n pt smaller, where n is the height of the keyboard - 216 in portrait, 162 in landscape.)
The scroll view should deal with letting your content scroll correctly after you've changed it's frame.
The content size property is the size of the thing that is being scrolled - this doesn't change, it's just the amount that you can see that has changed.
(If you're feeling flash, you can animate the change in height of the frame :)