I have two UIButtons in my app, which are custom buttons with background images, so they look like two dice. When my app is run on something not-iPhone 5 (not 4.0 inch screen) I want the to resize the buttons. I already have the code that checks for non-iPhone 5 devices(and it works), I just need to know how to change the size of the UIButtons.
I have tried lots of answers for similar questions, but so far I have not written any code that actually changes the size of my UIButtons.
I have written something like this in the "viewDidLoad" inside my "check-if-screen-is-non-iPhone-5"-statement:
button.frame = CGRectMake(90, 200, 100, 100);
The answer will depend on whether you're using Interface Builder or creating yoru button programatically. Most likely, you want to set the "content mode" of the button's imageView; then update the button's frame.
Here is an example:
UIImage *image = [UIImage imageNamed:#"close"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake( 0, 0, image.size.width, image.size.height);
button.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[button.imageView setContentMode:UIViewContentModeScaleAspectFit];
[button setImage:image forState:UIControlStateNormal];
Good luck.
Related
Could any one help me i want to add same image multiple times horizontally with same height and width. Important thing is i am creating image view dynamically i want to use same image view for all images! This is image i want to make horizontally like this but only one row needed like this.
You could achieve this by using stretchableImageWithLeftCapWidth:
UIImage *backgroundImage = [[UIImage imageNamed:#"SheetBackground.png"] stretchableImageWithLeftCapWidth:0.5 topCapHeight:0];
As per your request:
UIImage *backgroundImage = [[UIImage imageNamed:#"q4Ses.png"] stretchableImageWithLeftCapWidth:0.5 topCapHeight:0];
[_scro setBackgroundColor:[UIColor colorWithPatternImage:backgroundImage]];
And using your image:
The output is:
You can set this image on top of either UIScrollview, UIView and buttons. You do not need a for loop for that.
UPDATE:
The above code is for filling the entire background. If you wish to add only for one row then you have to create one UIView and set its colorWithPatternImage like below:
UIImage *backgroundImage = [[UIImage imageNamed:#"q4Ses.png"]
stretchableImageWithLeftCapWidth:1 topCapHeight:0];
UIView *v=[[UIView alloc]
initWithFrame:CGRectMake(0, 0, _scro.frame.size.width, 45)];
[v setBackgroundColor:[UIColor
colorWithPatternImage:backgroundImage]];
[_scro addSubview:v];
And the output:
Make a view of the height of image. But this view can have any width.
Then set your tile image in this view with following code.
UIImage *tiledImage = [UIImage imageNamed:#"myTiledImage.png"];
self.view.backgroundColor = [UIColor colorWithPatternImage:tiledImage];
This will get you the image tiled multiple times horizontally.
If the view spreads the image everywhere on screen then you'll have to add the following code to your view
self.view.clipToBounds = YES;
UIScrollView *myScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
CGFloat scrollWidth = 0.f;
for (int i=0;i<10;i++)//i=10,put as many image number u want to display
{
imageView = [[UIImageView alloc] initWithFrame:
CGRectMake(scrollWidth, 0, 80, 60.f)];
imageView.image=[UIImage imageNamed:#"urimagename"];
imageView.tag=i;
[myScrollView addSubview:imageView];
scrollWidth += 100;
}
myScrollView.contentSize = CGSizeMake(scrollWidth, 100);
EDIT:
You can achieve this in one more way.
CodenameLambda1's answer is better than the above one.But still some changes needs to be done in the #CodenameLambda1's answer..as the SOP's requirement is to display it in scrollview.So instead of self.view use scrollview.
UIScrollView *vie=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];
UIImage *tiledImage = [UIImage imageNamed:#"login"];
vie.backgroundColor = [UIColor colorWithPatternImage:tiledImage];
vie.contentSize=CGSizeMake(1400,60);
vie.clipsToBounds = YES;
[self.view addSubview:vie];
I tried tintColor = [UIColor redColor] and it does the job, but it only show the expected color when the button is being pressed.
In Coloration of UIButtons of type UIButtonTypeRoundedRect they answer the same question but you need an image to implement it, is there any way to do it without an image?
Here is what i want: image
Here is what i'm getting using images: image
Remember... NO IMAGES!
well using an image is one way of doing it, but i guess you already clear that in your question that you do not wish to use an image. here is button that i created and colored programmatically.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100,50);
[btn setTitle:#"Hello" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor colorWithRed:128.0/255.0f green:0.0/255.0f blue:0.0/255.0f alpha:0.7]];
btn.frame = CGRectMake(100.0, 100.0, 120.0, 50.0);//width and height should be same value
btn.clipsToBounds = YES;
btn.layer.cornerRadius = 20;//half of the width
btn.layer.borderColor=[UIColor redColor].CGColor;
btn.layer.borderWidth=2.0f;
[self.view addSubview:btn];
you can change the [UIColor ColorWithRed: wo whatever you want or even use [UIColor redColor] or any other things you want to do with this.
here is a screen shot:
adding a background color to rounded button is not possible, change the type of the button to custom and then use quartz core to set a round button. so add that to your project and import it in the .m and it should work. hope this works for you man.
to change the button to something similar to the image you showed change the frame to :
btn.frame = CGRectMake(50.0, 100.0, 200.0, 50.0);
and change the radius to :
btn.layer.cornerRadius = 7;
that should do it.
You can create a method to generate an UIImage from a UIColor and then set the returned image as the background of the button, for example:
- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
UIImage *image = [self imageWithColor:[UIColor colorWithRed:151.0/255.0
green:36.0/255.0
blue:40.0/255.0
alpha:1.0]];
[self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.button setBackgroundImage:image forState:UIControlStateNormal];
self.button.layer.cornerRadius = 6.0;
self.button.layer.borderWidth = 1.0;
self.button.layer.borderColor = [UIColor darkGrayColor].CGColor;
self.button.clipsToBounds = YES;
Using this I get
here's how I should do it:
download a button image from the internet or create an image yourself, to make it look like you want it to look like. Photoshop might be your best shot for this. make sure you have a transparent background and save it as a .png.
then add it to your xcode project. to make the button:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:#"yourImage.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(buttonAction)
forControlEvents:UIControlEventTouchDown];
[btn setTitle:#"Use it!" forState:UIControlStateNormal];
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
btn.titleLabel.font = [UIFont systemFontOfSize:20];
btn.frame = CGRectMake(position and size of your button);
[self.view addSubview:btn];
yourImage.png needs to be replaced with the name of your image.
The buttonAction is the action your button performs when it's tapped. if you just add:
- (void) buttonAction
{
}
anywhere in your file, anything you added between those brackets will be performed.
Make sure the size of your button is the same as the size of your CGRect, to make sure it doesn't look stretched out or anything you don't want it to look like.
If you have any questions, feel free to ask them.
Hope this helped you
I have a problem with UIButton image if I set its imageView contentMode to UIViewContentModeScaleAspectFill.
UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
imageButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
[imageButton setImage:[UIImage imageNamed:#"myImage.jpg"] forState:UIControlStateNormal];
[imageButton addTarget:self action:#selector(doSmth:) forControlEvents:UIControlEventTouchUpInside];
The image inside button is properly scaled to fill the whole button area.
After a click/touch on button the image gets resized (flickers) like if it's contentMode is set to UIViewContentModeScaleAspectFit.
Does anyone know how to remove this flickering when click/touch occurs?
Thanks!
Why are you setting content mode of the button's imageView? The image, you set for specified state, is always filling the size of the button. So you have to set button's frame size to the size of the image and not to set imageView contentMode
The flickering could be the higlighting of the button. If you want to set custom image for highlighting, use this code:
[imageButton setImage:buttonHightlightImage forState:UIControlStateHightlighted]
Just disable/uncheck the highlighted adjusts image in attributes inspector for imageButton
i'm creating a button programmaticly for an iPad application. when i see the button, there looks to be a shadow type thing below it. what is it and how can i get rid of it?
here is the code that creates it:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.titleLabel.font = [UIFont fontWithName:#"Trebuchet MS" size:12];
[myButton setTitle:#"test" forState:UIControlStateNormal];
myButton.frame = CGRectMake(0, 0, self.leftScrollView.frame.size.width, 50);
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"gear12.png"]];
[myButton addSubview:myImageView];
[self.leftScrollView addSubview:myButton];
UPDATE:
ok, i notice i only get that effect when its in my scrollview. if i add it to the view, no shadow effect.
the top test button, the button is a subview of the view. the bottom button is a subview of the scrollview which is a subview of the view (button/view vs button/scrollview/view).
white section is the view, grey is the scrollview/view.
UPDATE 2:
as pointed out by robmayor, UIButtons always have that double line effect, just not noticeble when the background color is white. the blue is a view and the grey is the subview scrollview.
This question is old (6 months) but i'have found a solution to delete/mask this bad effect of double lines.
[yourButton.layer setBackgroundColor: [[UIColor blackColor]CGColor]];
[yourButton.layer setBorderWidth:1.0f];
[yourButton.layer setBorderColor:[[UIColor blackColor]CGColor]];
[yourButton.layer setShadowOpacity:0.1f];
[yourButton.layer setCornerRadius:10];
UIColor selected is depending of the current background of your view.
Result :
On iPads, a rounded-rect UIButton always draws a white line along its bottom edge. You can't see that white line if the button's superview is white, but it's still there.
You have a few options:
Make the superview white. This is the easiest but you might not like the way it looks.
Make some rounded rect images in your favorite image editor. Set the button type to custom and set your rounded rect images as the button's images.
Make a subclass of UIButton and override its drawRect: method.
Set the button type to custom and use the button's layer properties (button.layer.backgroundColor, button.layer.borderColor, button.layer.borderWidth, button.layer.cornerRadius) to give the button a rounded rect appearance. You'll have to update button.layer.backgroundColor when the button is touched if you want it to turn blue like a normal one does. (Actually a normal one uses a blue gradient.)
Replace [UIButton buttonWithType:UIButtonTypeRoundedRect] with:
UIButton *myButton = [UIButton new];
or:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
You want to have your customized button. You can still make it with rounded corners if needed.
I'm trying to make a button that basically mimics the buttons seen on the iphone's home screen - it has a picture placed vertically above some text, and both are part of the button and behave appropriately (dim, clickable, etc) when highlighted.
I've tried putting the picture or the text into its own frame but that doesn't work because whichever is in the new frame doesn't dim with the rest of the button.
Using the background image property doesn't work because I want the image above the text - the area behind the text should be transparent. I would REALLY prefer a programming solution instead of going in and editing all my pictures.
Create a subclass of UIView, and have a UIImageView and UILabel as subviews, and position them appropriately using code.
You'd then have the UIImageView and UILabel as properties so that you could access them, like this:
myCustomButton.imageView.image = [UIImage imageNamed:#"..."];
myCustomButton.textLabel.text = #"...";
Or you could probably find a 3rd Party custom UIView, on the internet on github or something.
Jonathan's idea is a good one, but if you're only doing it with a couple images it's not worth the trouble. You can create a UIImage and a UILabel and then place them inside a UIButton in interface builder.
Still, it's always a good idea to learn about powerful features like subclassing.
Good luck,
Aurum Aquila
do this:
UIButton *btn_chat = [UIButton buttonWithType:UIButtonTypeCustom];
btn_chat.frame = CGRectMake(40, 50, 100, 30);//CGRectMake(20, 20, 200, 72);
UIImage *image = [UIImage imageNamed:#"chat.png"];
CGSize newSize = CGSizeMake(30, 30);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[btn_chat setImage:newImage forState:UIControlStateNormal];
btn_chat.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
[btn_chat setTitle:#"Chat" forState:UIControlStateNormal];
btn_chat.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
btn_chat.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[btn_chat addTarget:self action:#selector(menuSelection:) forControlEvents:UIControlEventTouchUpInside];
[btn_chat setTag:1001];
[self.view addSubview:btn_chat];
and it's done :)
Create a new initially transparent bitmap drawing context, draw your image to the top and text to the bottom. Then make a new image from this drawing context, and use that new image for the button.