I have an image (small button) which looks perfect in Portrait Orientation, but my whole App is running in Landscape Right mode, however the image does not change to landscape.
I want to do it programmatically
I tried:
NSString *pathToImg = [[NSBundle mainBundle] pathForResource:#"DetonatorSmall" ofType:#"png"];
UIImage *img = [UIImage imageWithContentsOfFile:pathToImg];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
imgView.transform = CGAffineTransformMakeRotation(3.14159265/2);
[button setImage:imgView.image forState:UIControlStateNormal];
But the image is still in portrait.
Any ideas what I can do?
Try This:
UIImage *img = [UIImage imageWithContentsOfFile:pathToImg];
[button setImage:img forState:UIControlStateNormal];
[button setTransform:CGAffineTransformMakeRotation(-M_PI / 2.0)];//Left
[button setTransform:CGAffineTransformMakeRotation( M_PI / 2.0)];//Right
I fixed it myself! :)
I added the image to a button, before it was drawn, so the button was the one I had to move. I applied the .transform to the imageview, but I had to do it fro the button :)
Can be closed
Related
Im having trouble trying to implement my own design to a tab bar. Somehow the items and background don't align correctly even tho they have the same height. The buttons seem to be further up than the background and i can't figure out why.
Code for implementing the items/buttons:
(this is added in my "viewDidLoad" in "Tab1ViewController.m", wich is the first of my 5 view controllers)
UIImage *selectedImage0 = [UIImage imageNamed:#"searchSEL"];
UIImage *unselectedImage0 = [UIImage imageNamed:#"search"];
UIImage *selectedImage1 = [UIImage imageNamed:#"homeSEL"];
UIImage *unselectedImage1 = [UIImage imageNamed:#"home"];
UIImage *selectedImage2 = [UIImage imageNamed:#"dareSEL"];
UIImage *unselectedImage2 = [UIImage imageNamed:#"dare"];
UIImage *selectedImage3 = [UIImage imageNamed:#"starSEL"];
UIImage *unselectedImage3 = [UIImage imageNamed:#"star"];
UIImage *selectedImage4 = [UIImage imageNamed:#"friendsSEL"];
UIImage *unselectedImage4 = [UIImage imageNamed:#"friends"];
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
UITabBarItem *item4 = [tabBar.items objectAtIndex:4];
[item0 setTitle:#""];
[item1 setTitle:#""];
[item2 setTitle:#""];
[item3 setTitle:#""];
[item4 setTitle:#""];
[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
[item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
[item2 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:unselectedImage2];
[item3 setFinishedSelectedImage:selectedImage3 withFinishedUnselectedImage:unselectedImage3];
[item4 setFinishedSelectedImage:selectedImage4 withFinishedUnselectedImage:unselectedImage4];
and to add the background i just added this code to my didFinishLaunchingWithOptions in my appDelegate.m
UITabBar *tabBar = [UITabBar appearance];
[tabBar setBackgroundImage:[UIImage imageNamed:#"bg"]];
The buttons are 64x49px and the background is 320x49px.
I wont be able to answer to or read comments in a while, but I will as soon as i can, hopefully someone can help.
Well, figured it out and it was kind of obvious. The part under the items are where the title should be. And since my icons just took up the icon space and not the title space, there was a emptiness under where the title would be.
Solved the problem by creating a whole background image for the bar and specified borders and gradient on that instead and just created the icons separately ass small white/black images and transparent background.
I have this UIButton, he has an image, on normal iPhone it works fine, the image contentMode is set to UIViewcontentModeCenter and its fine. but on retina, the image is not center, it's big and doesn't fit into the button frame.
Why is that and how can I fix it?
[theImageView setImage:Image forState:UIControlStateNormal];
[theImageButton setImage:Image forState:UIControlStateHighlighted];
[theImageButton setImage:Image forState:UIControlStateSelected];
[theImageButton setBackgroundColor:[UIColor clearColor]];
[theImageButton addTarget:self action:#selector(dismissKeyboard:) forControlEvents:UIControlEventAllTouchEvents];
theImageButton.imageView.contentMode = UIViewContentModeCenter;
The image loading:
- (UIImage *)loadScreenShotImageFromDocumentsDirectory
{
UIImage * tempimage;
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/MeasureScreenShot.png", docDirectory];
tempimage = [[UIImage alloc] initWithContentsOfFile:filePath];
return tempimage;
}
I have to say, that the image that loaded into the contentMode is a screen shot that has been taken programmatically. maybe it has to do with it (anyway on normal display it works fine).
Thanks!
Sounds like you're getting a retina image, with 1.0 scale. You need to tell the system, that your image is actually 2.0, if you use contexts to capture screenshot, use
UIGraphicsBeginImageContextWithOptions(..., ..., 0.0);
0.0 tells it to use current device's main screen scale, or you can specify correct scale yourself.
I want to set image as a background on my button. So i create an image:
then I want to implement it as stretchable:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 200, 400)];
[button setTitle:#"Test Button" forState:UIControlStateNormal];
UIImage *buttonImage = [_model.background resizableImageWithCapInsets:UIEdgeInsetsMake(6, 8, 6, 7)]; //this is screen 1
//UIImage *buttonImage = _model.background; //this is screen 2
[button addTarget:self action:#selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self addSubview:button];
I want to background stretch like in screen 2 but corners will sharp like screen 1. _model.background is UIImage contains original button background from the top of this post.
Am I using it right? I follow the documentation so UIEdgeInsetsMake(top,left,bottom,right) should be unstreachable corners, right?
According to the docs, the part inside the insets (the part not locked via cap settings) is tiled. So you should have a small 1 pixel region to tile. E.g. make the insets be UIEdgeIndetsMake(20,8,19,7)
This produces:
honestly, this is not the best picture for this effect but, please, try this line:
[button setBackgroundImage:[[UIImage imageNamed:#"image.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(20.f, 62.f, 20.f, 62.f)] forState:UIControlStateNormal];
I am trying to change the image of a Tab Bar in my application. When I changed the image it was giving me a blank image instead.
Can I set .png format image?
Is it possible to change the color of the tab bar?
Can I use a colored image in the tab bar?
No, You can use .icon images for tabbar which has transparent background and black and white. It is possible to set color of tabbar also.
tabbar Color
CGRect frame = CGRectMake(0.0, 0, 320, 48);
UIView *v = [[UIView alloc] initWithFrame:frame];
[v setBackgroundColor:[UIColor colorWithRed:0.2 green:0.8 blue:0.4 alpha:0.3]];
//[v setAlpha:1.0];
[[tabbar tabBar] insertSubview:v atIndex:0];
[v release];
in iOS5 all of those things are possible!
UIImage *selectedImage0 = [UIImage imageNamed:#"TabBa1selected.png"];
UIImage *selectedImage1 = [UIImage imageNamed:#"TabBa2selected.png.png"];
UIImage *selectedImage2 = [UIImage imageNamed:#"TabBa3selected.png.png"];
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:selectedImage0];
[item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:selectedImage1];
[item2 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:selectedImage2];
UIImage* tabBarBackground = [UIImage imageNamed:#"tabBarBackground.png"];
UIImage* tabBarSelected = [UIImage imageNamed:#"SelectedImage.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:tabBarSelected];
You would typically set an image for your tab bar in the AppDelegate class. It likely has a tab bar controller defined as _tabBarController, so the code would be:
[[_tabBarController tabBar] setBackgroundImage:[UIImage imageNamed:#"imageName.png"]];
If the image provided is of the proper size or stretchable it will be that image, otherwise it will be the image tiled however many times it takes to fill the tab bar.
You could also change the tab color if you'd like.
I've this code for adding UIActivityIndicatorView to a UITableViewCell.
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
UIImage *spacer = [UIImage imageNamed:#"spacer"];
UIGraphicsBeginImageContext(CGSizeMake(220, 150));
[spacer drawInRect:CGRectMake(0,0,spinner.frame.size.width,spinner.frame.size.height)];
UIImage* resizedSpacer = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.image = resizedSpacer;
[cell.imageView addSubview:spinner];
[spinner startAnimating];
Currently the image is shown in the top left of the 220x150 box. Instead I would like to put it in center. How can I do that? I've tried to change the two first parameters on CGRectMake, but that didn't change anything.
You've added resizedSpacer as the imageView without actually doing anything to spinner's frame. You want something like:
CGSize spinSize = spinner.frame.size;
spinner.frame = CGRectMake((220-spinSize.width)/2., (150-spinSize.height)/2.,spinSize.width, spinSize.height);
[cell.imageView addSubview:spinner];
I don't think that the logic involving spacer is required. In fact, you may not even want to host the UIActivityIndicatorView in the UIImageView at all; why not position it directly within the tableview cell?