Setting the size of a UIView - iphone

I have a UIViewController subclass to control a UIView that I want to add to a UIScrollView. I only want the view to be 100px high, but when I add it to the scroll view it gets made 460px high, ignoring the frame size I set:
MyViewController *vc = [[MyViewController alloc] init];
vc.view.frame = CGRectMake(0, 0, 320, 100);
myScrollView.autoresizesSubviews = NO
[myScrollView addSubview:vc.view];
[vc release];
I have set the scroll view to not autoresize subviews but it seems this is still happening! What can I do?
I have also tried setting the frame size inside loadView: in the UIViewController (which is where I will add all my controls and will need access to the size of the view) but that doesnt work either!
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor redColor];
self.view.frame = CGRectMake(0, 0, 320, 100); // still doesnt work
}
Any help would be greatly appreciated!

You are using loadView incorrectly, im even suprised you see a view (you shouldnt since in load view you arent assigns the vc view to anything), in loadView you must assign your view to a new UIView i nstance, anyway, you should be doing the same but in viewDidLoad instead of load view, that might work for you

Here is a snippet of how I do it. Note that the origin is with respect to the view you are adding to (in my case 'self').
appRect.origin=CGPointMake(0.0, 0.0);// origin
appRect.size = CGSizeMake(320.0f, 100.0f); //size
CGRect frame = CGRectInset(appRect, 0.0f, 0.0f);
gv=[[GraphicsView alloc] initWithFrame:appRect object:[model me]];
[gv setFrame:frame];
[self.view addSubview:gv];
[gv release];

Related

UIScrollview ContentSize magic

I am zooming an UIImageView in a UIScrollView on double tap. Works fine when it is set up in Interface Builder. The magic that I see here is, the contentSize of the scrollView increses/decreases by itself as I zoom in/out. I check for the contentSize in viewDidLoad, its not zero.
I tried the same by removiing the scrollView and imageView from IB and created them programmatically. After adding the imageView and tapGestureRecognizer, I fail to zoom here. When I checked for the reason, the contentSize remains zero everywhere.
When I add them in IB/xib, I am doing nothing with the contentSize. I am not setting it anywhere. I find its woking fine, the contentSize adjusts automatically where I need. But, its is not replicating when I create the scrollView programmatically.
How Can I make it work? I welcome your suggestions.
Here is my code for the reference.
ScrollView created in IB
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[imageView addGestureRecognizer:doubleTap];
imageScrollView.minimumZoomScale=1.01;
imageScrollView.maximumZoomScale=5;
imageScrollView.zoomScale=1.01;
NSLog(#"height = %f width = %f", imageScrollView.contentSize.height, imageScrollView.contentSize.width);
}
NSLog says
height = 449.479767 width = 320.000000
ScrollView created programmatically
- (void)viewDidLoad {
[super viewDidLoad];
imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[imageView addGestureRecognizer:doubleTap];
imageScrollView.minimumZoomScale=1.01;
imageScrollView.maximumZoomScale=5;
imageScrollView.zoomScale=1.01;
[self.view addSubview:imageScrollView];
[self.imageScrollView addSubview:imageView];
NSLog(#"height = %f width = %f", imageScrollView.contentSize.height, imageScrollView.contentSize.width);
}
NSLog says
height = 0.000000 width = 0.000000
These properties
self.minimumZoomScale=1.01;
self.maximumZoomScale=5;
self.zoomScale=1.01;
should be properties of your scrollView:
imageScrollView.minimumZoomScale=1.01;
imageScrollView.maximumZoomScale=5;
imageScrollView.zoomScale=1.01;
Then additionally you need to set the contentSize property (when using IB you don't need to set it as it is loaded from the xib file):
imageScrollView.contentSize = CGSizeMake(width,height);
using appropriate values.
Additionally you need to set the delegate:
imageScrollView.delegate = self;
And at least one delegate method indicating which subview to zoom:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return scrollView.subviews[0];
}
(actually you are better off setting a tag on the subview or assigning it to a property to get a reference to it, as scrollViews also have built-in subviews)
Is auto layout enabled in your xib? If so, IB is setting up constraints betwixt the image view and the scroll view, and at runtime auto layout uses the constraints to set the scroll view's content size. When you create the views in code, you're not adding enough constraints and not setting the content size directly, so the content size remains zero.
Read Tech note TN2154 for more information.
The similar answer is given by #Pheel about the zooming of image inside a scrollview using coding.
The link of the answer is given below:
IOS: add imageview in a scrollview to have zoom

How to Set Alpha or opacity in old view only before addsubView

I don't know, How to set old view in interface to decrease alpha in old view only
not decrease New view
i have this function
-(IBAction)save:(id)sender
{
testView * subTestview = [[testView alloc]initWithNibName:#"TestView" bundle:nil];
subTestview.view.frame = CGRectMake(0, 0, subTestview.view.frame.size.width,
subTestview.view.frame.size.height);
subTestview.view.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
self.view.alpha = 0.3f;
[self.view addSubview:subTestview.view];
}
when i use "self.view.alpha = 0.3f", it decrease alpha of all view
Help me please
Thank you
Ton
I think, you should make another parent view. Put your testView on it. Put your subTestview on it also independently(not as a subview of testView, but as a subview of parent view). Try to set alpha of your testView after that.
Try following code to add view controller as subview in current view
CurrentViewController.m
PopUpViewController *popUpViewController = [[PopUpViewController alloc] initWithNibName:#"PopUpViewController" bundle:nil];
popUpViewController.view.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.9f];
popUpViewController.view.frame = self.view.frame;
[self.view addSubview: popUpViewController.view];
[self addChildViewController: popUpViewController];

UIScrollView scrolls too far after initial zoom

My UIScrollView is populated with a large content and then zoomed with "setZoomScale:animated", such that it fits into the scrollview frame. The view appears properly zoomed out, and properly positioned in the scrollview. However, the user is able to scroll outside the content (the scrollview background color shows). It seems that he can scroll as far as the original content size (as if the content was not zoomed out).
Strangely enough, after the user zooms manually the first time, everything works fine, and the scrollview is constrained to the content size again.
I have searched the web, and gone through the Apple docs and examples, but could not find anyone having the same problem. The Apple examples seem to show the same thing as I do.
My content is a custom view that is derived from UIView, and whose CALayer is replaced by a CATiledLayer (as in the Apple examples). The drawing I do myself in drawLayer:inContext:.
Here is a code snippet from MyScrollViewController:
- (void)loadView {
CGRect frame = [UIScreen mainScreen].applicationFrame;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame];
scrollView.maximumZoomScale=1.0;
scrollView.delegate = self;
... setup theContentView ...
scrollView.contentSize = theContentView.bounds.size;
scrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
[scrollView addSubview:theContentView];
self.view = scrollView;
double zoomScale = 0.5; // hardcoded for testing
[scrollView setZoomScale:zoomScale animated:NO];
NSLog(#"zoomscale=%g contentview=%# contentsize=%#", zoomScale, NSStringFromCGSize(theContentView.bounds.size), NSStringFromCGSize(scrollView.contentSize));
[scrollView release];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return theContentView;
}
The NSLog statement shows a correctly set contentSize and zoomscale...
Hoping there is something obvious that I am missing...
I ended up setting the zoomScale on the next runloop (instead of directly from loadView), which worked:
- (void) loadView {
CGRect frame = [UIScreen mainScreen].applicationFrame;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame];
scrollView.maximumZoomScale=1.0;
scrollView.delegate = self;
... setup theContentView ...
scrollView.contentSize = theContentView.bounds.size;
scrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
[scrollView addSubview:theContentView];
self.view = scrollView;
// set the zoom level on the next runloop invocation
[self performSelector:#selector(initialZoom) withObject:nil afterDelay:0];
[scrollView release];
}
- (void) initialZoom {
double zoomScale = 0.5; // hardcoded for testing
UIScrollView *scrollView = (UIScrollView *) self.view;
[scrollView setZoomScale:zoomScale animated:NO];
}
Another possibility, suggested by Maverick1st, is to set the zoomscale from viewDidAppear, rather than LoadView. That approach has the added advantage that it shows an animation of the zooming, indicating to the user that the view starts in a zoomed state.
- (void) viewDidAppear {
double zoomScale = 0.5; // hardcoded for testing
UIScrollView *scrollView = (UIScrollView *) self.view;
[scrollView setZoomScale:zoomScale animated:YES];
}
I'm new at this but you could try clipping subviews. I created a scroll view with interface builder, and there in the attributes inspector was a checkbox clip subviews, so i think you should do something like that, only programmatically. Hope that helps.

UIScrollView and image thumbnails

I want to have a scroll view, where you display 5 thumbnails on the scroll view each time.
Basically it's a table with one row and in each row we have 5 thumbnails in it.
You slide your finger to the right then you display the next set of 5 thumbnails.
What do I need to do this?? Is it true that we need a scroll view to do this? How can I start?
In your UIView that will contain your UIScrollView, place (something like) this code in -viewDidLoad:
UIScreen *screen = [UIScreen mainScreen];
pageWidth = screen.bounds.size.height;
pageHeight = screen.bounds.size.width;
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, pageWidth, pageHeight)];
[scrollView setAutoresizingMask:UIViewAutoresizingNone];
[scrollView setContentSize:CGSizeMake(numPages * pageWidth, pageHeight)];
[scrollView setContentOffset:CGPointMake(pageWidth, 0)];
[scrollView setPagingEnabled:YES];
[scrollView setDelaysContentTouches:YES];
[scrollView setDelegate:self];
[scrollView setClearsContextBeforeDrawing:NO];
[scrollView setOpaque:YES];
[self.view insertSubview:scrollView atIndex:0];
[scrollView release];
// iterate through your thumbnails - which are UIViews
thumbnailViews = [NSMutableArray array];
[thumbnailViews retain];
NSUInteger pageCounter = 0;
for (...)
{
ThumbnailView *thumbnailView = [[ThumbnailView alloc] initWithFrame:CGRectMake( pageCounter*pageWidth, 0, pageWidth, pageHeight )];
[thumbnailView setMultipleTouchEnabled:YES];
[thumbnailView setViewController:self]; // this may be necessary to push/pop on navigation controller stack.
[thumbnailView restoreState];
[scrollView thumbnailView];
[thumbnailViews thumbnailView];
++pageCounter;
}
In my case, pageWidth, pageHeight, thumbnailViews and scrollView are all ivars for my UIView subclass.
I just looked in here as I'm learning about UIScrollViews myself. I started to try and get westsider's code to work, but I noticed that it is practically gobblygook. It should be in a UIViewController that implements the <UIScrollViewDelegate> protocol, and things like [scrollView thumbnailView]; is meaningless as here thumbnailView is a method name. I believe http://www.codeproject.com/Articles/46556/How-To-Use-UIScrollView-in-Your-iPhone-App.aspx is more coherent.
To answer the question, I think you want to create a UIScrollView in a standard manner--such as in the link, and you want to create a subclass of UIView called, say, UIView5 that has 5 UIImageViews. Then, instead of adding a normal UIView to the UIScrollView, like [scrollView addSubview:standardUIView];, you add UIView5 objects.

How to make UIScrollView respect the layout of the containing UIView?

I'm using a UIView to control the layout of my view (along with a view controller). I want UIScrollView to only use half of the vertical screen. That works fine if I use the upper half of the screen, but not the bottom half.
Here's the relevant code from the UIViewController:
- (void)loadView {
CGRect fullFrame = [[UIScreen mainScreen] applicationFrame];
//trying to put the scroll view on the bottom half of the screen, but does not work.
CGRect halfFrame = CGRectMake(0, fullFrame.size.height / 2 ,
fullFrame.size.width, fullFrame.size.height / 2);
//use this instead for the scroll view to go to the top half of the screen (and work properly)
//CGRect halfFrame = CGRectMake(0, 0 , fullFrame.size.width, fullFrame.size.height / 2);
UIScrollView* sv = [[UIScrollView alloc] initWithFrame:halfFrame];
[sv setContentSize:CGSizeMake(3 * halfFrame.size.width, halfFrame.size.height)];
CGRect stencilFrame = halfFrame;
UIView *leftView = [[UIView alloc] initWithFrame:stencilFrame];
stencilFrame.origin.x += stencilFrame.size.width;
UIView *centerView = [[UIView alloc] initWithFrame:stencilFrame];
stencilFrame.origin.x += stencilFrame.size.width;
UIView *rightView = [[UIView alloc] initWithFrame:stencilFrame];
//mix up the colors
[leftView setBackgroundColor:[UIColor redColor]];
[centerView setBackgroundColor:[UIColor greenColor]];
[rightView setBackgroundColor:[UIColor blueColor]];
//add them to the scroll view
[sv addSubview:leftView];
[sv addSubview:centerView];
[sv addSubview:rightView];
//turn on paging
[sv setPagingEnabled:YES];
UIView *containerView = [[UIView alloc]initWithFrame:fullFrame];
[containerView addSubview:sv];
[self setView:containerView];
}
Thank you in advance for any advice or help.
I figured it out. The crux of the problem is that views within the scroll view are initialized with the same frame as the scroll view itself. When the scrollView is initialized with halfFrame, the origin is (0, half the full screen size), which is ok since that is relative to the application window itself. However, the views that are put inside the scrollView (like leftView) are initialized to halfFrame, but in this case the origin is relative to the scrollView, effectively placing them off the screen. Setting the origin to (0,0) fixes this:
CGRect stencilFrame = CGRectMake(0, 0, fullFrame.size.width , fullFrame.size.height / 2);
contentSize must contain the rectangle of the view inside the scroll view. That is, the total size of all scrollable controls within. The frame of the UIScrollView decides how much scrolling is needed to let the user browse everything.
You don't have the "full frame" available if you have a nav bar or a tab bar. In general, code that uses [UIScreen mainScreen] for layout information is probably wrong.
Additionally, the status bar can change size if (for example) a call is in progress or tethering is enabled.
Instead, use any sane value for full frame and enable autoresizing:
CGRect fullFrame = {{0,0}, {320,480}};
...
sv.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
EDIT: You also probably need to subclass UIScrollView and implement -setFrame: so that it also sets the content size and -layoutSubviews to do the correct layout.