I've been spinning on this for 2 hours now, so thought I'd post here for some advice.
In my viewDidLoad method, I do:
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 600)];
...
UIView *contactsRow = [self generateContactsRow];
contactsRow.frame = CGRectMake(10, tableMaxY, 300, 56);
...
[scrollView addSubview:contactsRow];
[self.view addSubview:scrollView];
[scrollView release];
In [self generateContactsRow], I basically create a container view, load a bunch of other views (button, label, etc) inside, and return it.
UIView *containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 0)] autorelease];
... (define stuff)
// set it
[containerView addSubview:imageView];
[containerView addSubview:textLabel];
[containerView addSubview:detailTextLabel];
[containerView addSubview:addButton];
[containerView addSubview:hr];
// and forget it
[imageView release];
[textLabel release];
[detailTextLabel release];
[addButton release];
[hr release];
return containerView;
The fact that I'm autoreleasing containerView is causing my app to crash. I return an alloc'd object, so I thought I had to release it somehow, and autorelease seemed the best way. If I remove it, things work fine, but won't I then have a memory leak?
Thanks
Make the segment like the following
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 600)];
...
UIView *contactsRow = [[self generateContactsRow] retain];
contactsRow.frame = CGRectMake(10, tableMaxY, 300, 56);
...
[scrollView addSubview:contactsRow];
[self.view addSubview:scrollView];
[contactsRow release];
[scrollView release];
I am retaining the contactsRow here and releasing that after I have it as subview to the scrollView. So there won't be any memory leak.
You can release it in viewdidunload or viewwillDisapper methods...
Related
When this Action is executed it displays totally black view screen with toolbar
- (void)displayviewsAction:(id)sender
{
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
self.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:toolbar];
UIView *viewController = [[UIView alloc] init];
UIView *secondController = [[UIView alloc] init];
[self.view insertSubview:viewController atIndex:1];
[self.view insertSubview:secondController atIndex:2];
NSLog(#"%d", [[self.view subviews] indexOfObject:viewController]); // Is 1
NSLog(#"%d", [[self.view subviews] indexOfObject:secondController]); // Is 2
[self.view bringSubviewToFront:viewController];
NSLog(#"%d", [[self.view subviews] indexOfObject:viewController]);
[self.view sendSubviewToBack:viewController];
NSLog(#"%d", [[self.view subviews] indexOfObject:viewController]);
[self.view bringSubviewToFront:secondController];
NSLog(#"%d", [[self.view subviews] indexOfObject:secondController]);
SecondViewController * secondController = [[[SecondViewController alloc] init]autorelease];
secondController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:secondController.view];
[self.view addSubview:toolbar];
}
Any ideas why the view is appearing as totally black with toolbar.
The default background color of views is nil, which is a transparent background.
If you have nothing in any of the views and set no background color for any of them, the default background of the window or the device will show through. I didn't see it in the documentation, but I am assuming it is black.
Also, you have defined UIView pointers, but named them with the word "controller". This can contribute to mistakes since a UIViewController is an entirely different object. The UIViewController contains a UIView and interacts with it, but it is not a UIView, nor does it inherit from one.
i want to place a list of uiviews instead of uiimageviews as a content in uiscrollview for my ipad app.i got images scrolling by using imageviews, but i need uiviews as a list of subviews that has to be scroll horizontally?i tried adding uiviews in uiscrollview through Interfacebuilder ,but while running the app i can see only one uiview where as all remaining views are not there. please help me out of this.
I tried it like that, still it's not showing the view, and also I tried programmatically for dimensions of UIScrollView and views inside it like the the code as below: –
scrollView.frame=CGRectMake(0, 604, 728,365);
scrollView.bounces = YES;
scrollView.pagingEnabled = YES;
scrollView.backgroundColor = [UIColor darkGrayColor];
scrollView.delegate = self;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.userInteractionEnabled = YES;
UIView *subView1=[[UIView alloc]initWithFrame:CGRectMake(20, 604, 758,365)];
subView1.backgroundColor=[UIColor blueColor];
[scrollView addSubview:subView1];
[subView1 release];
UIView *subView2=[[UIView alloc]initWithFrame:CGRectMake(828, 604, 758,365)];
subView2.backgroundColor=[UIColor blueColor];
[scrollView addSubview:subView2];
[subView2 release];
[scrollView release];
UIView *subView1=[[UIView alloc]initWithFrame:CGRectMake(20, 604, 758,365)];
subView1.backgroundColor=[UIColor blueColor];
[scrollView addSubview:subView1];
[subView1 release];
UIView *subView2=[[UIView alloc]initWithFrame:CGRectMake(828, 604, 758,365)];
subView2.backgroundColor=[UIColor blueColor];
[scrollView addSubview:subView2];
[scrollView setContentSize:CGSizeMake(758+758, 365+365)]]
[subView2 release];
[scrollView release];
[scrollView setContentSize:CGSizeMake(758+758, 365+365)]]
you need to set the contentsize to be able to scroll and see all subviews.
You need to add UIView as an when needed. you need to use programmatic approach to add UIView. You should have a look at PageControl sample code provided by Apple. I hope you get your answer.
i tried with [758+758,365]; . now it is scrolling horizontally but only first view i can see. second view is not coming .
scrollView.frame=CGRectMake(0, 604, 728,365);
scrollView.bounces = YES;
scrollView.pagingEnabled = YES;
scrollView.backgroundColor = [UIColor darkGrayColor];
scrollView.delegate = self;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.userInteractionEnabled = YES;
[scrollView setContentSize:CGSizeMake(1600,365)];
UIView *subView1=[[UIView alloc]initWithFrame:CGRectMake(20, 604, 758,365)];
subView1.backgroundColor=[UIColor blueColor];
subView1.frame=scrollView.bounds;
[subView1 addSubview:myImageView];
[scrollView addSubview:subView1];
UIView *subView2=[[UIView alloc]initWithFrame:CGRectMake(828, 604, 758,365)];
subView2.backgroundColor=[UIColor blackColor];
subView2.frame=scrollView.bounds;
[scrollView addSubview:subView2];
[subView1 release];
[subView2 release];
[scrollView release];
I have seen it done on some apps, where the navigation bar is actually smaller than the default 44px, and there is a UIView (which has functionality) above the nav bar...
I want more than a custom background image, which I did manage to figure out how to do, but I dont know where to start getting something like this done.
Any help is greatly appreciated :)
Mark
I found a sort of way to do this:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 20.0f, 320.0f, 32.0f)];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
UIImageView *back = [[UIImageView alloc] initWithImage: [UIImage imageNamed:#"logo.png"]];
[back setFrame: CGRectMake(0, 0, 320, 20)];
[tempView addSubview: back];
[[self view] addSubview: tempView];
[[self view] addSubview: navBar];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: #"Controls"];
[navBar pushNavigationItem:navItem animated:NO];
which seems to do the trick, although I cannot seem to figure out how to get this 'into' the navigationController so that back buttons work, at the moment I have to manually insert a leftBarButtonItem into the navItem, the back button never seems to show...
Yes you can,
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 20.0f, 320.0f, 32.0f)];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
navBar.navigationBar.layer.zPosition =-1;
self.view insertSubview:navBar atIndex:[[self.view subviews] count]];
[self.view insertSubview:tempView atIndex:[[self.view subviews] count]];
HI,
I develop an application in which I want to implement the splash screen, on that splash screen I want to bind the scrollView and UIImage. My code as follow,
-(void)splashAnimation{
window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
//scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scrollView = [[UIScrollView alloc] initWithFrame:[window bounds]];
scrollView.pagingEnabled = NO;
scrollView.bounces = NO;
UIImage *image = [UIImage imageNamed:#"splash.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.userInteractionEnabled = NO;
[scrollView addSubview:imageView];
[scrollView setDelegate:self];
//[scrollView release];
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[self splashAnimation];
[self initControllers];
[window addSubview:[mainTabBarController view]];
[window makeKeyAndVisible];
}
On my given code the one blank window comes up and stay on.
I want to on that blank screen bind my splash.png.
****The Above problem is solved****
My current code is
scrollView.pagingEnabled = NO;
scrollView.bounces = NO;
UIImage *image = [UIImage imageNamed:#"splash.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.userInteractionEnabled = NO;
[scrollView addSubview:imageView];
scrollView.maximumZoomScale = 4.0f;
scrollView.minimumZoomScale = 1.0f;
CGRect rect = CGRectMake(119, 42, 208, 166);
[scrollView zoomToRect:rect animated:YES];
[scrollView setDelegate:self];
[window addSubview:scrollView];
[window makeKeyAndVisible];
I want to zoom the particular part of scrollView.
RajB - for your zooming of the scrollView do this:
CGRect zoomRect = CGRectMake(119, 42, 208, 166);
[scrollView zoomToRect:zoomRect animated:YES];
Hope this helps!
You create a UIScrollView but never add it to the view hierarchy, therefore it will never get displayed. Call [window addSubview:scrollView], and then don't forget to release it.
If you are using a MainWindow.xib in your project, your window is created for you, you do not need to create your own.
Use [[UIScreen mainScreen] instead of CGRect(0, 0, 320, 420) <- I also believe you meant "480"
After setting up your splash animation, you call [window addSubview:[mainTabBarController view]]. Even after adding your scrollview as previously mentioned, this will then become the topmost and therefore visible view.
Delay the [window addSubview:[mainTabBarController view]] until after your splash animation completes.
I am trying to add the UIButton to UIScrollView.
I have added the UIImageView with background image *(size: 320 * 620)*.
Then I added this UIImageView to UIScrollView, and It works fine.
But now I want to add UIButton at position at : (60, 500); (below screen that would appear after scrolling).
I have tried following code, but the button is added on UIView not on scrollview. Button is not getting displayed on the top.
Code :
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.hidden = TRUE;
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SingleModelVar.png"]];
self.imageView = tempImageView;
[tempImageView release];
imageView.userInteractionEnabled = YES;
scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; //fit to screen
//scrollView.delaysContentTouches = NO;
scrollView.contentSize = CGSizeMake(320, imageView.frame.size.height); //imageView.frame.size.height=620 here
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 0.75;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
//The Problem begins ..........
btnStartDate=[[UIButton alloc] initWithFrame:CGRectMake(60,500,100,20)];
[scrollView addSubview:btnStartDate];
//[self.view addSubview:btnStartDate];
btnEndDate=[[UIButton alloc] initWithFrame:CGRectMake(60,530,100,20)];
[scrollView addSubview:btnEndDate];
//[self.view addSubview:btnEndDate];
[scrollView addSubview:imageView];
[[self view] addSubview:scrollView];
}
it is not displayed at the top because you added them before you added the imageView.
Put [scrollView addSubview:imageView]; before btnStartDate=[[UIButton alloc] initWithFrame:CGRectMake(60,500,100,20)];