in landscape no showing image and button - ios

I have added one imageview, 2 button in one view controller in code. In portrait mode its working well. But in landscape mode not able to see the image and button.
#implementation ViewController {
UIImageView* imgView; // your UIImageView
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[self navigationController] setNavigationBarHidden:YES animated:YES];
UIImage* img1 = [UIImage imageNamed:#"11.jpg"];
UIImage* img2 = [UIImage imageNamed:#"s2.jpg"];
imgView = [[UIImageView alloc] initWithFrame:self.view.frame]; // create a UIImageView with the same size as the view.
imgView.animationImages = #[img1, img2]; // use a nice NSArray literal to pass in your images.
imgView.animationDuration = 2.0*2.0; // 2 seconds for each image (x3 images)
[self.view addSubview:imgView]; // add UIImageView to view
[imgView startAnimating]; // start animating
// button one
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height-50, self.view.frame.size.width/2 , 50.0)];
[button setBackgroundColor:[UIColor colorWithRed:188/255.0f green:155/255.0f blue:211/255.0f alpha:0.6f]];
[button setTitle:#"Sign Up" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; // title color
[button addTarget:self action:#selector(method:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// Button Two
UIButton* button1 = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height-50, self.view.frame.size.width/2 , 50.0)];
[button1 setBackgroundColor:[UIColor colorWithRed:188/255.0f green:155/255.0f blue:211/255.0f alpha:0.6f]];
[button1 setTitle:#"Sign In" forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(method:) forControlEvents:UIControlEventTouchUpInside];
[button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; // title color
[self.view addSubview:button1];
// Border color for one button
UIView *leftBorder = [[UIView alloc] initWithFrame:CGRectMake(0, 1, 1, button.frame.size.height)];
leftBorder.backgroundColor = [UIColor colorWithRed:237/255.0f green:211/255.0f blue:246/255.0f alpha:0.6f];
[button1 addSubview:leftBorder];
}
Why its not working well for landscape. Please help me, for portrait its working well. For landscape its not working ..

It's not working in landscape because when we rotate to landscape the button and image view, as you have positioned them in your code, are located below the bottom of the screen.

You will have to implement Auto Layout. It will fix the problem. Please avoid hard coding the positions as will create problems in UI. If you want to hard code the points then make your app in either portrait mode or landscape mode.
In auto layout constraints can be set programmatically as well. Just check which is feasible for you and implement it.

Related

Inactive part of the screen on fullscreen

I'm trying to put a picture on fullscreen with buttons on to do actions.
Here is a screenshot of my result:
My problem is that the buttons on the first line (the highest) are not touchable.
It seems to have no action on the top layout.
Here is my code:
- (void)showActionsForGlobalFullScreen:(UIGestureRecognizer *)gestureRecognizer {
//LEFT
UIImage* cancelImage = [UIImage imageNamed:#"cancel.png"];
CGRect frameimg = CGRectMake(3, 3, cancelImage.size.width, cancelImage.size.height);
UIButton *cancelButton = [[UIButton alloc] initWithFrame:frameimg];
UIImage* flagImage = [UIImage imageNamed:#"flag.png"];
CGRect frameimg2 = CGRectMake(3, 40, flagImage.size.width, flagImage.size.height);
UIButton *flagButton = [[UIButton alloc] initWithFrame:frameimg2];
[cancelButton addTarget:self action:#selector(closeFullView:) forControlEvents:UIControlEventAllTouchEvents];
[cancelButton setBackgroundImage:cancelImage forState:UIControlStateNormal];
[flagButton addTarget:self action:#selector(displayFlag:) forControlEvents:UIControlEventAllTouchEvents];
[flagButton setBackgroundImage:flagImage forState:UIControlStateNormal];
//RIGHT
UIImage *exitFullScreenImage = [UIImage imageNamed:#"exitFullScreen.png"];
CGRect frameimg3 = CGRectMake(fullScreenViewFromVC.frame.size.width - 25 , 3, exitFullScreenImage.size.width, exitFullScreenImage.size.height);
UIButton *exitFullScreenButton = [[UIButton alloc] initWithFrame:frameimg3];
UIImage *commentImage = [UIImage imageNamed:#"comment.png"];
CGRect frameimg4 = CGRectMake(fullScreenViewFromVC.frame.size.width - 25 , 40, commentImage.size.width, commentImage.size.height);
UIButton *commentButton = [[UIButton alloc] initWithFrame:frameimg4];
[exitFullScreenButton addTarget:self action:#selector(exitFullScreenPhoto:) forControlEvents:UIControlEventAllTouchEvents];
[exitFullScreenButton setBackgroundImage:exitFullScreenImage forState:UIControlStateNormal];
[commentButton addTarget:self action:#selector(diplayComments:) forControlEvents:UIControlEventAllTouchEvents];
[commentButton setBackgroundImage:commentImage forState:UIControlStateNormal];
//[actionsView addSubview:flagButton];
[fullScreenViewFromVC addSubview:cancelButton];
[fullScreenViewFromVC addSubview:flagButton];
[fullScreenViewFromVC addSubview:exitFullScreenButton];
[fullScreenViewFromVC addSubview:commentButton];
}
Important note:
fullScreenViewFromVC is an own class extending UIView and instantiated on my controller.
Basically I'm called the fullscreen method from a picture on a UITableViewCell
I'm using this fullScreenViewFromVC variable to store my data from the current selected cell to my controller.
Thank you for your help.
Finally it was because of another view that I had to hide.
I thought that changing de zPosition of my view in fullscreen should be enough but it did not.
Problem solved.

UIScrollView hiding UINavigationBar

I have an a UIControlView with the following hierarchy.
...
UIView
UIScrollview
textfield
button
textfield
button
NavigationBar
...
When this view loads the scrollview pushes the navigationBar off the screen or hides it? What am I doing wrong?
When I drag a srollview onto the UIViewController storyboard, should the scrollview be at 480 or around 430 so its below the navigationbar?
// === HERE IS THE CODE (there is no other custom code besides this) ======
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
DLog(#"%f", self.scrollView.contentOffset.y);
//self.scrollView.contentOffset = CGPointMake(self.view.contentOffset.x,0);
self.codeTextField.delegate = self;
self.mobileTextField.delegate = self;
[self.navBarLabel setTitleColor:[UIColor statDarkGray] forState:UIControlStateNormal];
[self.navBarLabel setTitleColor:[UIColor statDarkGray] forState:UIControlStateSelected];
[self.navBarLabel setTitleColor:[UIColor statLightGray] forState:UIControlStateHighlighted];
UIFont *font = [UIFont fontWithName:#"GothamRounded-Light" size:17];
[self.navBarLabel.titleLabel setFont:font];
//SETUP TEXTFIELDS
// --- Setup textfield designs ---------
self.codeTextField.borderStyle = UITextBorderStyleNone;
UIImage *fieldBGImage = [[UIImage imageNamed:#"code_textfield.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:20];
[self.codeTextField setBackground:fieldBGImage];
// for left padding
self.codeTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 88, 20)];
self.codeTextField.leftViewMode = UITextFieldViewModeAlways;
self.mobileTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 88, 20)];
self.mobileTextField.leftViewMode = UITextFieldViewModeAlways;
// setup submit button
self.submitButton.titleLabel.text = #"Submit";
self.submitButton.titleLabel.font = [UIFont fontWithName:#"GothamRounded-Light" size:18];
self.submitButton.enabled = NO;
[self.submitButton setBackgroundImage:[UIImage imageNamed:#"bluebutton_disabled.png"] forState:UIControlStateDisabled];
[self.submitButton setBackgroundImage:[UIImage imageNamed:#"bluebutton_normal.png"] forState:UIControlStateNormal];
[self.submitButton setBackgroundImage:[UIImage imageNamed:#"bluebutton_selected.png"] forState:UIControlStateSelected];
// setup submit button
self.resendCodeButton.enabled = NO;
self.resendCodeButton.titleLabel.font = [UIFont fontWithName:#"GothamRounded-Light" size:18];
[self.resendCodeButton setBackgroundImage:[UIImage imageNamed:#"redbutton_disabled.png"] forState:UIControlStateDisabled];
[self.resendCodeButton setBackgroundImage:[UIImage imageNamed:#"redbutton_normal.png"] forState:UIControlStateNormal];
[self.resendCodeButton setBackgroundImage:[UIImage imageNamed:#"redbutton_selected.png"] forState:UIControlStateSelected];
// SETUP BUTTONS
// The next arrow button
UIImage *background = [UIImage imageNamed:#"Cancel_normal.png"];
UIImage *backgroundSelected = [UIImage imageNamed:#"Cancel_selected.png"];
self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.cancelButton addTarget:self action:#selector(cancelPressed:) forControlEvents:UIControlEventTouchUpInside]; //adding action
[self.cancelButton setBackgroundImage:background forState:UIControlStateNormal];
[self.cancelButton setBackgroundImage:backgroundSelected forState:UIControlStateSelected];
self.cancelButton.frame = CGRectMake(0 ,0,background.size.width, background.size.height);
self.cancelButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.cancelButton];
self.navigationItem.leftBarButtonItem = self.cancelButtonItem;
}
The view was shown modally not pushed. And even though I designed a navbar and set one in the settings (translucent bar). It wasn't showing one?
So on the segue I was using show modally and the, no matter if it had a UIView or UIScrollView, never would show the navbar. By switching to a PUSH segue, it now shows the navbar. Fixed.
I just decided to push the view instead of modal to be done with it.

adding multiple subviews in ios

I'm trying to add multiple subview programmatically, but my buttons inside those subviews are not working. The buttons were also added as subviews programmatically.
Here's the code, hopefully will explain more about what I mean.
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
[self setBackgroundColor:[UIColor clearColor]];
// UIView container of selected image with comment button.
UIView *containerOfSelectedImage = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 0, 350)];
NSLog(#"%f", self.frame.size.width);
// image view of selected photo by user.
UIImageView *userImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"nature_01.jpg"]];
[userImage setFrame :CGRectMake(0, 0, 340, 300)];
// comment button with action of recodering user's comment.
UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
//[commentButton setBackgroundColor : [UIColor clearColor]];
float y = userImage.frame.size.height + 5.0f;
[commentButton setFrame : CGRectMake(32.0f, y, 24.0f, 24.0f)];
[commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchDown];
[commentButton setImage:[UIImage imageNamed:#"comments-24.png"] forState:UIControlStateNormal];
[containerOfSelectedImage addSubview:userImage];
[containerOfSelectedImage addSubview:commentButton];
[self addSubview:containerOfSelectedImage];
[self addSubView:self.commentContainerView];
return self;
You need to increase the frame of the containerOfSelectedImage. Currently the width is 0.
The containerOfSelectedImage's frame has to be big enough so the button fits inside it. If the button is outside that frame it will show up but you can't touch him.
To debug this issues you can use a tool like Reveal App. If any of your buttons is outside the frame of the parent then the touch will not be detected.
Change [commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchDown]; to [commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchUpInside];

Scrolling an Image with ImageView iOS

I'm trying to make an app for a restaurant and the to display the food menu as a scrollable image. The image is pulled form a config file.
- (void)viewDidLoad
{
[super viewDidLoad];
// back button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"btn-back.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f, 0.0f, 46.0f, 28.0f);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
[customBarItem release];
[self setTitle:[menu objectForKey:#"title"]];
[scrollView2 setScrollEnabled:YES];
NSString *imageName = [menu objectForKey:#"imageName"];
if (![imageName isEqualToString:#""]) {
UIImage *image = [UIImage imageNamed:imageName];
[imageView setImage:image];
[scrollView2 addSubview:imageView];
[imageView release];
}
[textView setText:[menu objectForKey:#"description"]];
}
As of now the image doesn't scroll, but appears as a static image. Could anyone please help me modify this code so the user can vertically scroll the image.
Thanks a lot.
You should use a UIScrollViewand add the UIImageView as subview of the contentView.
And then set the contentsize of the scrollview as the imageviews size.
[imageView setImage:image];
[scrollView2.contentView addSubview:imageView];
[scrollView2 setContentSize:image.frame.size];
[imageView release];
have no Mac to test at the moment but that should work.
I agree mostly with what Seega wrote. Your imageView must be subview of a scrollView. Then, don't forget to set the scrollView's contentSize & the minimum&maximumZoomScale & and the imageView's frame:
self.scrollView.contentSize = self.imageView.image.size;
self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
self.scrollView.minimumZoomScale = // some number between 0 and 1, default is 1, so no zoom
self.scrollView.maximumZoomScale = // some number larger than 1, default is 1, so no zoom
self.scrollView.zoomScale = 1 // starting zoom scale
A nice place to put this code is in
-(void)viewWillLayoutSubviews{}
Thus it will be executed everytime the view appears & also whenever the device is rotated.

UIButton inside UIImageView does not respond to taps

I have a scroll view, which has an image view with an image as a subview, and the image view has a UIButton as one of its subviews. The problem is, I am not able to click on the button. I can SEE the button, but I cannot tap on it.
Can someone tell me what is it that I am messing up? Any help is greatly appreciated! Thanks!
Below is the code:
scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"img.jpg"]];
scrollView.delegate = self;
self.view = scrollView;
// add invisible buttons
[self addInvisibleButtons];
[scrollView addSubview:imageView];
addInvisibleButtons has the following code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:#"point" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
[self.imageView addSubview:button];
UIImageView has userInteractionEnabled set to NO/ false by default.
You are adding the button as a subview to the image view. You should set it to YES / true.
May I ask why are you adding invisible UIButtons to UIImageView?
Seems like a bad practice, notice Interface Builder doesn't allow you to add UIButton inside them.
If you want an image with touch handling, you can:
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:#"point" forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"img.jpg"] forState:UIControlStateNormal];
[button setFrame:CGRectMake(0.0, 0.0, 40.0, 40.0)];
[scrollView addSubview:button];
You can have addInvisibleButtons implementation as
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundColor:[UIColor clearColor]];
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:#"point" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
self.imageView.userInteractionEnabled = YES;
[self.imageView addSubview:button];
If you want to have UIButton as completely invisible then you need to remove a line
[button setTitle:#"point" forState:UIControlStateNormal];
since it use to show text on UIButton which make it visible.
This may resolve your issue.

Resources