I want the presentview and its contents to be on scroll view when orientation occurs to landscape mode - objective-c

when i shift from portrait mode to landscape mode the view should expand widely but the length should be the same and in scrollview so that when i scroll the fullcontents should be displayed:
if (([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeLeft) ||
([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeRight))
{
UIScrollView *scrollView=[[UIScrollView alloc] init];
UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 480, 480)];
view1 = self.view;
[scrollView addSubview:view1];
scrollView.frame = CGRectMake(0, 0, 480, 480);
scrollView.contentSize = CGSizeMake(480, 480);
scrollView.delegate = self;
scrollView.alwaysBounceVertical = YES;
[self.view addSubview:scrollView];
}

if(([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeLeft) || ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeRight)) {
UIScrollView * scrollView=[[UIScrollView alloc] init]; UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 480, 480)];
view1 = self.view; [scrollView addSubview:view1]; scrollView.frame = CGRectMake(0, 0, 480, 480);
scrollView.contentSize = CGSizeMake(480, 480); scrollView.delegate = self;
scrollView.alwaysBounceVertical = YES; [self.view addSubview:scrollView];
}

Please update your question with more information for example - what is and is not working? Don't add updates as comments, edit your question.
In your existing code, at least this is wrong/confused:
UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 480, 480)];
view1 = self.view;
you are assigning a pointer to a new view in the first line, then reassigning it to your existing self.view in the second line. Then here:
[self.view addSubview:scrollView];
you are adding a scrollview containing a content view to that same content view. That's not going to work.
As a cleaner solution, why don't you set up your scrollview/subview combo for both orientations. Then when you rotate, you just need to alter your scrollview's contentsize.
For the normal upright orientation:
self.scrollview.contentSize = self.scrollview.bounds.size;
For landscape:
self.scrollview.contentSize =
CGSizeMake(self.scrollView.bounds.size.width, self.contentView.bounds.size.height
(assuming your content is in a view called contentView contained inside your scrollView)
You should be able to get the contentOffset to work correctly for both orientations in the storyboard, but if not your can set it in code for each orientation.

Related

Disable interaction on upper view

I have two views in my program. One is statusView, it's alpha is set to 0.8f. And below it is tableView. How do I disable statusView such way, that clicking on it would not click tableView. Settings userInteractionEnabled = false; didn't help.
Here is how i add views.
[self.view addSubview:_builded.view]; // obj with my tableView
info = [[Info alloc] init];
[self.view addSubview:info.view]; // statusView
If you add a subview like the following, the tableView underneath would not catch the user interaction.
UITableView *testTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
testTableView.delegate = self;
testTableView.dataSource = self;
[self.view addSubview:testTableView];
UIView *statusView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
statusView.backgroundColor = [UIColor redColor];
statusView.alpha = 0.8;
[self.view addSubview:statusView];

how to set orientation for a view which is subviewed to window

I have created a UIView.I want to subview the view to the appdelegate window
UIView *newView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1048, 748)];
AppDelegate *appdelegate = [[UIApplication sharedApplication]delegate];
[appdelegate.window addSubview:newView];
This helps me to subview a view to the window.But the view is in portrait mode.I need the view to be in landscape mode .How can I set the view to the landscape mode? and I want the Red color view to completely cover the white view.how can I do so?
RedColor is the newView
white is present viewController
if we add any object to window and then we want to change the orientation then we must use transform methods.
#define DegreesToRadians(degrees) (degrees *M_PI /180)
add above line
CGAffineTransform newTransform;
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
switch (orientation)
{
case UIInterfaceOrientationPortraitUpsideDown:
newTransform = CGAffineTransformMakeRotation(-DegreesToRadians(180));
txt.transform = newTransform;
txt.frame = CGRectMake(0, 0, 320, 480);
break;
case UIInterfaceOrientationLandscapeLeft:
newTransform = CGAffineTransformMakeRotation(DegreesToRadians(-90));
txt.transform = newTransform;
txt.frame = CGRectMake(0, 0, 320, 480);
break;
case UIInterfaceOrientationLandscapeRight:
newTransform = CGAffineTransformMakeRotation(DegreesToRadians(90));
txt.transform = newTransform;
txt.frame = CGRectMake(0, 0, 320, 480);
break;
default:
newTransform = CGAffineTransformMakeRotation(-DegreesToRadians(0));
txt.transform = newTransform;
txt.frame = CGRectMake(0, 0, 320, 480);
break;
}
here txt is object name,try in this way.

Using subviews in scrollerview iPhone

I am trying to add uipickerview in uiscrollview, but it is not being shown properly, just a black square.
Further, I have to add 1 more uipickerview, some labels and some buttons.
The code I am using is as under:
-(void)loadView {
[super loadView];
UIPickerView *pView = [[UIPickerView alloc] initWithFrame:CGRectMake(20, 20, 280, 165)];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Background.png"]];
scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroller.pagingEnabled = YES;
NSInteger numberOfViews = 2;
for (int i = 0; i < numberOfViews; i++) {
CGFloat yOrigin = i * self.view.frame.size.height;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
// awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroller addSubview:awesomeView];
[scroller addSubview:pView];
}
scroller.contentSize = CGSizeMake(self.view.frame.size.width , self.view.frame.size.height * numberOfViews);
[self.view addSubview:scroller];
}
what can be the issue?
Are you putting data in the picker view? I mean, if you just put the picker view without data on it you are not going to see anything on it.
You need to add the delegate and the datasource and put some data in your picker!
pView.delegate = self;
pView.datasource = self;
and then you have to implemente at least:
numberOfComponentsInPickerView:
numberOfRowsInComponent:
titleForRow:
make sure you are implementing uipickerviewdelegate methods. its normal that it will show just the black square because your pickerview cant find the data and thus does not display anything.
UIPickerView *pView = [[UIPickerView alloc] initWithFrame:CGRectMake(20, 20, 280, 165)];
Change height to 216 .. UIPickerView has fixed height of 216 ..only.. you can't change it..

How to programmatically add UIView in a scrollview in ios?

I used the below codes
CGRect scrollserviceViewFrame1 = CGRectMake(60,610, 650, 150);
//CGRect scrollViewFrame =[[UIScreen mainScreen] applicationFrame];
//UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
UIScrollView *servicescrollView1 = [[UIScrollView alloc] initWithFrame:scrollserviceViewFrame1];
[self.view addSubview:servicescrollView1];
//scrollView.backgroundColor=[UIColor greenColor];
[servicescrollView1 setBackgroundColor:[UIColor redColor]];
[servicescrollView1 setCanCancelContentTouches:NO];
//scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
servicescrollView1.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
servicescrollView1.scrollEnabled = YES;
servicescrollView1.autoresizesSubviews=YES;
[servicescrollView1 setContentSize: CGSizeMake(1050, 100)];
//scrollView.delegate = self;
servicescrollView1.pagingEnabled = YES;
servicescrollView1.showsHorizontalScrollIndicator = NO;
[servicescrollView1 scrollRectToVisible:CGRectMake(servicescrollView1.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height) animated:YES];
NSLog(#" scroll view width :%i ",servicescrollView1.frame.size.width);
NSLog(#"scroll view height :%i ",self.view.frame.size.height);
NSLog(#"self view width :%i",self.view.frame.size.width);
CGRect CGRectZero = CGRectMake(60,710,200, 100);
UIView* myserviceView1 = [[UIView alloc] initWithFrame: CGRectZero];
myserviceView1.backgroundColor=[UIColor blackColor];
[servicescrollView1 addSubview:myserviceView1];
[myserviceView1 release];
[servicescrollView1 release];
The scroll view is created , butcannot add the UIVIew to scrollview...
Any suggestions for solving this query???
Thanks in advance
Near the bottom of your code where you create myserviceView1, you (attempt to) reset a constant (CGRectZero).
And then when you create your myserviceView1 UIView, you're giving it a frame size of {0, 0, 0, 0}.

Rotating a full screen UIImageView

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/