UIScrollView start another position - iphone

I have project is single view app and add to view controller scrollview
with parameters
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(0, 960)];
if screen resolution is 320x480 then we have some invisible "screen 2" (320x480)
how do I make that - did load app at position "screen 2" and after that
I can scroll not down, but to up at start launch app.
How to release that?

you have to give the scrollview a proper content size, depending on the screen size.
Assuming that the scrollview is fullscreen in portrait:
//------------------------------------------
- (void)viewDidLoad{
[super viewDidLoad];
CGRect screenSize = [[UIScreen mainScreen] bounds];
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(0, screenSize.size.height)];
}
//------------------------------------------
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
CGRect screenSize = [[UIScreen mainScreen] bounds];
CGPoint scrollPoint = CGPointMake( 0.0, screenSize.size.height / 2);
[scroller setContentOffset:scrollPoint animated:YES];
}

- (void)viewDidLoad
{
self.YourSecondView.frame = CGRectMake("As You Need");
[self.ScView addSubview:self.YourSecondView];
CGRect rect = CGRectMake(self.YourSecondView.frame.origin.x,self.YourSecondView.frame.origin.y,self.YourSecondView.frame.size.width,self.YourSecondView.frame.size.height);
[self.ScView scrollRectToVisible:rect animated:YES];
}

Related

What piece am I missing to set an iOS background?

I have:
[A series of if statements sets backgroundImage to be something like [UIImageNamed #"Background-Default-Landscape"]; am I right to assume that the device will look for the file named Background-Default-Landscape#2x.png if it is a Retina display?]
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
CGRect containerRect = CGRectZero;
containerRect.size = [backgroundImage size];
UIView *containerView = [[UIView alloc] initWithFrame:containerRect];
[containerView addSubview:backgroundView];
Right now my app is launching and once it loads displays a white screen, not a background specified (none of the backgrounds are solid white).
What am I missing to draw a background before I start putting things on the background, further below in the code?
Thanks,
--EDIT--
As far as code, I have:
- (void) renderScreen
{
int height = [[UIScreen mainScreen] bounds].size.height;
int width = [[UIScreen mainScreen] bounds].size.width;
UIImage *backgroundImage = nil;
if (height == 2048 || height == 2008 || height == 1024 || height == 1004 || height == 984)
{
backgroundImage = [UIImage imageNamed:#"Background-Default-Portrait"];
}
// Other such statements cut.
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
CGRect containerRect = CGRectZero;
containerRect.size = [backgroundImage size];
UIView *containerView = [[UIView alloc] initWithFrame:containerRect];
[containerView addSubview:backgroundView];
[self.view addSubview:containerView];
Then below that are statements to draw on the background.
This method is called when the initial view loads and when it is rotated:
- (void)viewDidLoad
{
[super viewDidLoad];
[self renderScreen];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(renderScreen) name:UIDeviceOrientationDidChangeNotification object:nil];
}
The behavior is that the correct initial screen loads, then it fades to white and nothing interesting seems to happen after that.
--EDIT--
At the top, I have:
int height = [[UIScreen mainScreen] bounds].size.height;
int width = [[UIScreen mainScreen] bounds].size.width;
When I NSLog the height and width, for a retina device in landscape mode, I get a height of 1024 and a width of 768. The image displayed is a rotation of the portrait image; if I turn the device on the side the image neatly fills the whole screen but as-is the background displays a horizontally squeezed image.
Should I do anything different to be correctly obtaining height and width? I would expect for a device in landscape orientation the height would be 768 and the width would be 1024.
Every time renderScreen fires it will add two more subviews to the current controller's main view:
[containerView addSubview:backgroundView];
[self.view addSubview:containerView];
And this fires every time you rotate the device.
At least have the containerView as a property and replace it.
#property (nonatomic, strong) UIView *containerView;
and inside - (void) renderScreen
[self.containerView removeFromSuperview];
self.containerView = [[UIView alloc] initWithFrame:containerRect];
[self.view addSubview:self.containerView];
You could also add:
[self.view setNeedsDisplay];
So that the view is redrawn.

Animated UIPickerView not rotating in landscape mode

I have a UIPickerView that animates up and down in a UITableViewController. I adapted an example from apple (DateCell). The UIPickerView is created programmatically, no nib files.
In Portrait mode everything looks nice. When I rotate the simulator (I not testing on the device), the UITableView rotates well, but the picker remains were it was. I read tons of threads about that topic and many developers seem to have problems with pickers behaving weird in landscape mode but at least their pickers rotate. I made a subclass of UIPickerView as described in this link: http://www.llamagraphics.com/developer/using-uidatepicker-landscape-mode, but it didn't help for the rotation issue.
I tried to rotate the picker with a transform but it looked very weird, like broken.
I'm suspecting that the problem is that I'm using the picker inside of a UITableViewController, and so I add it as a subview of self.view.window. If I try to add the picker as a subview of self.view, only a white frame (without any picker) appears.
Any suggestions?
The initialization code in the UITableViewController subclass:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.pickerView.superview == nil)
{
//Initialization code
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGRect initFrame = orientation == UIInterfaceOrientationPortrait ? CGRectMake(0, 0, 320, 200) : CGRectMake(0, 0, 480, 160);
self.pickerView = [[RotatingUIPickerView alloc] initWithFrame:initFrame];
[self.pickerView setDelegate:self];
[self.pickerView setShowsSelectionIndicator:YES];
[self.pickerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
[self.view.window addSubview:self.pickerView];
// compute the start frame
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGSize pickerSize = [self.pickerView sizeThatFits:CGSizeZero];
CGRect startRect = CGRectMake(0.0,
screenRect.origin.y + screenRect.size.height,
pickerSize.width, pickerSize.height);
[self.pickerView setFrame:startRect];
// compute the end frame
CGRect pickerRect = CGRectMake(0.0,
screenRect.origin.y + screenRect.size.height - pickerSize.height,
pickerSize.width,
pickerSize.height);
// start the slide up animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
// we need to perform some post operations after the animation is complete
[UIView setAnimationDelegate:self];
[self.pickerView setFrame:pickerRect];
[UIView commitAnimations];
}
}
Implementation of UIPicker subclass as described on the link above:
- (id)initWithFrame:(CGRect)frame
{
if (self == [super initWithFrame:frame])
{
for (UIView * subview in self.subviews)
{
[subview setFrame:self.bounds];
}
}
return self;
}
- (id) initWithCoder:(NSCoder *)aDecoder
{
if (self == [super initWithCoder: aDecoder])
{
for (UIView * subview in self.subviews)
{
[subview setFrame:self.bounds];
}
}
return self;
}
By executing:
[self.view.window addSubview:self.pickerView];
you are adding pickerView as a subview of your UIWindow. I don't know how the main UIWindow can be rotated, if it can be.
When you are adding pickerView to the view
[self.view addSubview:self.pickerView];
you get problems due to the view being a UITableView.
What I suggest is adding pickerView to some intermediate UIView, added as subview to UIWindow and to which you add the UITableView. This intermediate UIView would also have a UIViewController associated to it so that shouldAutorotateToInterfaceOrientation can return the proper value to have auto rotation working.

MPMoviePlayerController Done button unresponsive in Landscape mode

All,
The Done button in MPMoviePlayerController dismisses the control in portrait mode.
However, both the Done and Toggle full screen button become unresponsive when i rotate to landscape.
My app is a very very simple app and just has didRotatefromInterfaceOrientation method where i change the movie frame width and height to landscape and change the origin to match landscapemode.
`- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
// Update the frame of the view.
CGRect newFrame = [[UIScreen mainScreen] applicationFrame];
newFrame.origin.x = - (newFrame.size.width / 2);
newFrame.origin.y = - (newFrame.size.height / 2);
[[self view] setBounds:newFrame];
[[self view] setCenter:CGPointMake( [[self view] bounds].size.width / 2, [[self view] bounds].size.height / 2)];
[self view].userInteractionEnabled = YES;
// Update the frame of the movie player.
newFrame = [[UIScreen mainScreen] applicationFrame];
if( fromInterfaceOrientation == UIInterfaceOrientationPortrait || fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
newFrame.size.width = newFrame.size.height;
newFrame.size.height = [[UIScreen mainScreen] applicationFrame].size.width;
}
newFrame.origin.x = - (newFrame.size.width / 2);
newFrame.origin.y = - (newFrame.size.height / 2);
[[[self moviePlayer] view] setFrame:newFrame];
[[[self moviePlayer] view ] setUserInteractionEnabled:YES ];
}`
Well the problem here was that i was rotating the view and resizing it in `- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation.
You are not required to do that as the movie player automatically does that for you.
Resizing and rotating the frame actually got the button confused and hence did not receive touch events when tapped.

UIPicker first appears black

I have the following code to draw a custom picker. Unfortunately when the view is first drawn is black. Only when I touch it does it appear.
How can I fix this?
Here is the relevant code in the UIView:
- (void)drawRect:(CGRect)rect
{
[self createPicker];
[self addSubview:dPicker];
//[dPicker reloadComponent:1];
}
-(void) createPicker
{
dPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
CGSize pickerSize = [dPicker sizeThatFits:CGSizeZero];
dPicker.frame = [self pickerFrameWithSize:pickerSize];
dPicker.delegate=self;
dPicker.showsSelectionIndicator = YES;
dPicker.hidden=NO;
}
- (CGRect)pickerFrameWithSize:(CGSize)size
{
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGRect pickerRect = CGRectMake( 0.0,
screenRect.size.height - 44.0 - size.height,
size.width,
size.height);
return pickerRect;
}
Fixed it. Called the subview from UIViewController instead of the the UIView

Problem zooming into UIImageView

I am following Apple's ScrollSuite example, however pinch to zoom is not working. I am not sure whats wrong:
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
// set up main scroll view
imageScrollView = [[UIScrollView alloc] initWithFrame:[[self view] bounds]];
[imageScrollView setBackgroundColor:[UIColor blackColor]];
[imageScrollView setDelegate:self];
[imageScrollView setBouncesZoom:YES];
[[self view] addSubview:imageScrollView];
// add touch-sensitive image view to the scroll view
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"default.png"]];
[imageView setTag:ZOOM_VIEW_TAG];
[imageView setUserInteractionEnabled:YES];
[imageScrollView setContentSize:[imageView frame].size];
[imageScrollView addSubview:imageView];
[imageView release];
// calculate minimum scale to perfectly fit image width, and begin at that scale
float minimumScale = [imageScrollView frame].size.width / [imageView frame].size.width;
[imageScrollView setMinimumZoomScale:minimumScale];
[imageScrollView setZoomScale:minimumScale];
//[self.imageView loadImageFromURL:self.url];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
if (enableLandscapeOrientation) {
[[self navigationController] setNavigationBarHidden:UIInterfaceOrientationIsLandscape(interfaceOrientation) animated:YES];
return YES;
}
else {
return NO;
}
}
#pragma mark UIScrollViewDelegate methods
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [imageScrollView viewWithTag:ZOOM_VIEW_TAG];
}
/************************************** NOTE **************************************/
/* The following delegate method works around a known bug in zoomToRect:animated: */
/* In the next release after 3.0 this workaround will no longer be necessary */
/**********************************************************************************/
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
[scrollView setZoomScale:scale+0.01 animated:NO];
[scrollView setZoomScale:scale animated:NO];
}
#pragma mark Utility methods
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {
CGRect zoomRect;
// the zoom rect is in the content view's coordinates.
// At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
// As the zoom scale decreases, so more content is visible, the size of the rect grows.
zoomRect.size.height = [imageScrollView frame].size.height / scale;
zoomRect.size.width = [imageScrollView frame].size.width / scale;
// choose an origin so as to get the right center.
zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
return zoomRect;
}
Double check your value for minimumScale. Based on that code and guessing at what "default.png" is, I'd say your minimumScale will likely be 1.0 which means your scroll view has no zoom range. Try doing this just to see if it works:
[imageScrollView setMinimumZoomScale:0.5f];
[imageScrollView setMaximumZoomScale:2.0f];