I'm having an issue with my app. I've got an splash with a UIImageView "Default-Landscape.png" The code is:
- (void)showLoadingWithTitle:(NSString*)loadingTitle {
if(![self.view viewWithTag:123456]) {
UIImageView *overlayView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//overlayView.backgroundColor = [UIColor lightGrayColor];
overlayView.tag = 123456;
overlayView.autoresizingMask = UIViewAutoresizingFlexibleWidth+UIViewAutoresizingFlexibleHeight;
overlayView.image = [UIImage imageNamed:#"Default-Landscape.png"];
}
}
So far, so good. The problem appears when I rotate the iPad to Portrait, I think that Portait is loading the Landscape image since it does not look like it should, I don't know what should I do... ¿any ideas?
EDIT: Actually even more simply...
- (void)showLoadingWithTitle:(NSString*)loadingTitle {
if(![self.view viewWithTag:123456]) {
UIImageView *overlayView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//overlayView.backgroundColor = [UIColor lightGrayColor];
overlayView.tag = 123456;
overlayView.autoresizingMask = UIViewAutoresizingFlexibleWidth+UIViewAutoresizingFlexibleHeight;
if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) ||
([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight))
{
overlayView.image = [UIImage imageNamed:#"Default-Landscape.png"];
} else
{
overlayView.image = [UIImage imageNamed:#"Default-Portrait.png"];
}
}
}
Related
I want to change the image in validation like if the text field is empty the image color is grey or if filled the circle is green color.
I have tried this in button action-
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
imgView.image = [UIImage imageNamed:#"a_image.png"];
imgView.contentMode = UIViewContentModeCenter;
Try this in your button action.
if (self.MyTextField.text.length!=0)
self.MyImageView.image=[UIImage imageNamed:#"redImage"];
else
self.MyImageView.image=[UIImage imageNamed:#"greenImage"];
If you want to change colour when user is typing in text field, use following delegate method of textfield to do any stuff.
-(void)textFieldDidBeginEditing:(UITextField *)textField;
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
if (textField.text.length == 0){
imgView.image = [UIImage imageNamed:#"firstImage.png"];
} else {
imgView.image = [UIImage imageNamed:#"secondImage.png"];
}
Should be pretty simple:
if (textField.text.length == 0){
//Do something if its empty
} else {
//Do something else if its not
}
EDIT: If you want to do this on the fly and not when they're done editing you could do something like:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.textField addTarget:self action:#selector(textChanged) forControlEvents:UIControlEventEditingChanged];
}
- (void)textChanged
{
if (self.textField.text.length == 0) {
self.someVIew.backgroundColor = [UIColor grayColor];
} else if (self.textField.text.length > 5){
self.someVIew.backgroundColor = [UIColor redColor];
} else {
self.someVIew.backgroundColor = [UIColor greenColor];
}
}
i used to use the code below to change the UINavigationBar Background :
the CODE:
- (void) drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed:#"NavBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
so now i need more than one background NavBar2.png ....
how to achieve this ?
Thanks
Try this code.
- (void) drawRect:(CGRect)rect
{
if(navigationBar.tag == 0){
UIImage *image = [UIImage imageNamed:#"NavBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
else if(navigationBar.tag == 1)
UIImage *image = [UIImage imageNamed:#"NavBar1.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
}
Tag logic mentioned by R.A. would definitely help you.
If you are developing an app on iOS5, then you can also take a look at :
- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics
I solve it by using my original code as image background then put titleView for the navigation bar
UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 165, 44)];
UIImageView *imgv = [[UIImageView alloc] initWithFrame:aView.frame];
imgv.image = [UIImage imageNamed:#"NavBar01.png"];
[aView addSubview:imgv];
self.navigationController.navigationBar.topItem.titleView = aView;
[aView release];
[imgv release];
I have UI issue with Kal Calendar for iPad. On the iPad there is an empty space but on the iPhone it's fine. How can i get it to fit in the frame on the iPad?
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
[kal.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
[kal.view setFrame:CGRectMake(0, 0,768 ,1004)];
}
I tried to use the code listed above but it did not work for me!
in KalGridView.m you'll find this.
const CGSize kTileSize = { 46.f, 44.f };
I'd change the code to a property where you can set the frame dynamically to the idiom and/or orientation.
in KalGridView.m
const CGSize kTileSize = { 109.0f, 109.0f };
and in KalView.m
- (void)addSubviewsToHeaderView:(UIView *)headerView
…
for (CGFloat xOffset = 0.f; xOffset < headerView.width; xOffset += 109.f, i = (i+1)%7) {
CGRect weekdayFrame = CGRectMake(xOffset, 30.f, 109.f, kHeaderHeight - 29.f);
UILabel *weekdayLabel = [[UILabel alloc] initWithFrame:weekdayFrame];
weekdayLabel.backgroundColor = [UIColor clearColor];
weekdayLabel.font = [UIFont boldSystemFontOfSize:10.f];
weekdayLabel.textAlignment = UITextAlignmentCenter;
weekdayLabel.textColor = [UIColor colorWithRed:0.3f green:0.3f blue:0.3f alpha:1.f];
weekdayLabel.shadowColor = [UIColor whiteColor];
weekdayLabel.shadowOffset = CGSizeMake(0.f, 1.f);
weekdayLabel.text = [weekdayNames objectAtIndex:i];
[headerView addSubview:weekdayLabel];
[weekdayLabel release];
}
}
results in:
I have two images:
Help-Portrait.png (320 x 480)
Help-Landscape.png (480 x 320)
When a user clicks the help button on any view, they need to be presented with the correct image, which should also rotate when the device does. I have tried adding the imageView to both the window, and the navigation controller view.
For some reason I am having issues with this.
Could anyone shed light on what I am doing wrong?
UIImage *image = nil;
CGRect frame;
if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) {
image = [UIImage imageNamed:#"Help-Portrait.png"];
frame = CGRectMake(0, 0, 320, 480);
} else {
image = [UIImage imageNamed:#"Help-Landscape.png"];
frame = CGRectMake(0, 0, 480, 320);
}
if (!helpImageView) {
helpImageView = [[UIImageView alloc] initWithFrame:frame];
helpImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
helpImageView.image = image;
}
[[UIApplication sharedApplication] setStatusBarHidden:YES];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(helpImageTapped:)];
helpImageView.userInteractionEnabled = YES;
[helpImageView addGestureRecognizer:tap];
[self.view addSubview:helpImageView];
[tap release];
willRotateToInterfaceOrientation:
if(helpImageView) {
[(id)[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
helpImageView.image = [UIImage imageNamed:#"Help-Portrait.png"];
} else {
helpImageView.image = [UIImage imageNamed:#"Help-Landscape.png"];
}
}
When you rotate the device the image and the frame don't change, and you end up with two thirds of the portrait image displayed on the left part of the screen.
What I want is it for it to show the correct image for the orientation, the right way up. Also I would like animation for the image rotation, but thats a side issue
The place where you need to adjust your button image is in your ViewController's shouldAutorotateToInterfaceOrientation method (documentation linked for you).
Do something like:
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
UIImage *image = NULL;
if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
{
image = [UIImage imageNamed:#"Help-Portrait.png"];
} else {
image = [UIImage imageNamed:#"Help-Landscape.png"];
}
[yourButton setImage: image forState: UIControlStateNormal]
return YES;
}
Michael Dautermann's answer looks to have almost all the answer, but I'm opposed to using shouldAutorotateToInterfaceOrientation. This method is designed only to determine if a rotation should or should not occur, nothing else.
You should use either didRotateFromInterfaceOrientation: willAnimateRotationToInterfaceOrientation:duration instead.
didRotateFromInterfaceOrientation: - interfaceOrientation is already set on your UIViewController so you can get the current orientation. In this case the rotation animation is already complete.
willAnimateRotationToInterfaceOrientation:duration - The benefit of this method is execution time. You are inside the rotation animation so you won't have the less than pretty effects which happens when you change UI either after the rotation animation completes.
Got it working, with this code:
- (void)showHelpImage {
NSString *imageName = #"Help_Portrait.png";
CGRect imageFrame = CGRectMake(0, 0, 320, 480);
helpImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
helpImageView.frame = imageFrame;
[self.view addSubview:helpImageView];
[self updateHelpImageForOrientation:self.interfaceOrientation];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(helpImageTapped:)];
helpImageView.userInteractionEnabled = YES;
[helpImageView addGestureRecognizer:tap];
[self.view addSubview:helpImageView];
[tap release];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self updateHelpImageForOrientation:toInterfaceOrientation];
}
- (void)updateHelpImageForOrientation:(UIInterfaceOrientation)orientation {
NSString *imageName = nil;
CGRect imageFrame = helpImageView.frame;
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
imageName = #"Help_Portrait.png";
imageFrame = CGRectMake( 0, 0, 320, 480);
} else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
imageName = #"Help_Landscape.png";
imageFrame = CGRectMake( 0, 0, 480, 320);
}
helpImageView.image = [UIImage imageNamed:imageName];
helpImageView.frame = imageFrame;
}
Got the idea from:
http://www.dobervich.com/2010/10/22/fade-out-default-ipad-app-image-with-proper-orientation/
I'm trying to understand how to add a label with a UISwitch or other controller to a footer (or header) in a sectioned tableView. Any help would be greatly appreciated. Thank you in advance!
Okay, after searching and working at it I've done the following:
// Need to refactor so that the label is Public Sharing and Priviate Sharing and the actions work for each switch
- (UIView *) tableView: (UITableView *) tableView
viewForFooterInSection: (NSInteger) section
{
if (section == 0 || section == 1) {
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
UIView* footerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, 44.0)] autorelease];
footerView.autoresizesSubviews = YES;
footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
footerView.userInteractionEnabled = YES;
footerView.hidden = NO;
footerView.multipleTouchEnabled = NO;
footerView.opaque = NO;
footerView.contentMode = UIViewContentModeScaleToFill;
// Add the label
UILabel* footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(150.0, -5.0, 120.0, 45.0)];
footerLabel.backgroundColor = [UIColor clearColor];
footerLabel.opaque = NO;
footerLabel.text = #"Sharing";
footerLabel.textColor = [UIColor tableHeaderAndFooterColor];
footerLabel.highlightedTextColor = [UIColor tableHeaderAndFooterColor];
footerLabel.font = [UIFont boldSystemFontOfSize:17];
footerLabel.shadowColor = [UIColor whiteColor];
footerLabel.shadowOffset = CGSizeMake(0.0, 1.0);
[footerView addSubview: footerLabel];
[footerLabel release];
// Add the switch
UISwitch* footerSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(215.0, 5, 80.0, 45.0)];
[footerView addSubview: footerSwitch];
// Return the footerView
return footerView;
}
else return nil;
}
// Need to call to pad the footer height otherwise the footer collapses
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
switch (section) {
case 0:
case 1:
return 40.0;
default:
return 0.0;
}
}
I hope this is correct and if this helps anyone else please vote this up. Cheers!
i think you need to autorelease the uiview-