UIView resize evenly - iphone

I have a uiview subclass that i want to expand evenly. I mean I want the origin point of the view to be in the center, and have the width and height expand the same amount. Right now the origin point is in the top left, (0,0), and it expands down to the right. How would I go about doing this?

When you change the width and height of the frame, also change the x and y position of the frame by half those amounts.
For example, if your frame is currently (40, 60, 100, 200) and you want it to be wider by 20 and higher by 30, make it (30, 45, 120, 230).

I suggest, if I understand you problem that you get the size of the superview in which the view is located. Let this view be view. Then, let your view be myView.
So all you have to do is the following :
Take half of the size of the screen for width and height values, and to position the origin of your view.
UIView view = myView.superview;
if (nil != view) {
// Make sure your view won't extend by itself with this
myView.autoresizingMask = UIViewAutoresizingNone;
// Get The size of the parent view
CGSize size = view.bounds.size;
// Set the new frame of your view
myView.frame = CGRectMake(size.width/2.0f,
size.height/2.0f,
size.width/2.0f,
size.width/2.0f);
}
And now your view is positioned correctly

Related

view width is not changing when i rotate the view

i have created a view in textfield should characters method like this. when i change my view from portrait to landscape my view width is not changing.
viewForautoCompleteTableView = [[UIView alloc]initWithFrame:CGRectMake(30, 120, 212, 100)];
viewForautoCompleteTableView.autoresizingMask=UIViewAutoresizingFlexibleWidth;
[self.view addSubview:viewForautoCompleteTableView];
viewForautoCompleteTableView.layer.borderWidth=1;
viewForautoCompleteTableView.layer.borderColor=[UIColor grayColor].CGColor;
thank you
Note that :
On Rotation, self.view.frame size does not change but self.view.bounds does , and bounds represent correct values with respect to current Interface Orientation.
There are two points that you have to check
. Is your view( viewForautoCompleteTableView) allocated ?
. If you want to change the width while rotation then you have to change the width of your view in didRotateFromInterfaceOrientation:UIInterfaceOrientation)fromInterfaceOrientation method
-(void)didRotateFromInterfaceOrientation: (UIInterfaceOrientation)fromInterfaceOrientation{
// put the condition to check for your orientation type i.e. portrait or landscape
CGSize newSize= CGSizeMake(newWidth,height); // setting new width here
CGRect viewFrame=viewForautoCompleteTableView.frame;
viewFrame. size = newSize;
viewForautoCompleteTableView.frame=textFrame; // provide some animation if required
}
Note:- Please make sure your view is allocated

Center multiple UIViews in a row its superview?

How would I go about placing arrays of identical sized uiviews at their superviews center?
For example: I have an array of (x number) of 50x50 sized views.. that I place right next to each other in a line. How can I center that line of 50x50 views, to the center of their superview?
Is there an easier way than what I'm thinking I'd have to do?
My approach would to take the count of views inside the array, and multiply it by 50. This would give me the width of the images together.
Then subtract that width from the width of the superview. Then place the subviews based off that math.
Is there an easier approach to go about doing this?
What you are suggesting is one way of doing it. Another way that has the advantage of keeping the views centered when you rotate the device is to do this:
Create a basic UIView to use as a container
Set the container bounds property to (0, 0, 50 * x, 50)
Layout the 50x50 subviews inside the container so they fill it
Add the container view as a subview of the main view
Set the center property of the container to the center of the superview
Set the autoresizingMask property of the container view to UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin
Use the center property of the UIView
Apple documentation says that: The center property can be used to adjust the position of the view without changing its size.
So you would do something like this:
Create a UIView that will contain all the 50x50 Views with a width of [array count] x 50;
Then add all the 50x50 views to this Container View.
Add this view to the desired view and then set its center point to its superview center point.
Something like this:
[my50x50ContainerView setFrame:CGRectMake(0.0, 0.0, [50x050ViewsArray count] x 50, 50.0)];
[self.view addSubview:my50x50ContainerView];
my50x50ContainerView.center = self.view.center;

iPhone CGRect - Get Frame Size?

I'm looking to find out the "lower limit" of a frame. The frame in question is a UITextView that resizes based on the size of the content e.g.
CGRect frame = mainTextBox.frame;
frame.size.height = mainTextBox.contentSize.height;
mainTextBox.frame = frame;
This works fine but it is the last item on a scrollView. So what I want to do is program the scroll view to only be as long as nessisary e.g.
[scrollView setContentSize:CGSizeMake(320,1850)]
What I want to do is make the 1850 the right size so that the scrollView finishes just below the mainTextBox.
Is there anyway to get a return as to the distance of the bottom of the frame to the top of the view?
Just add the y coordinate of the origin point (upper left corner of the view) to the height.
CGFloat lowerBound = mainTextBox.frame.origin.y + mainTextBox.frame.size.height;
would CGRectGetMaxY(mainTextBox.frame); work for your needs?

UIScrollView contentSize Issue - With example code

Within my view I create a scrollview with a width of 320 and a height of 70.
Responding to the user touching a button, I expand the scrollview, so it is 380(h) x 320(w) in size.
The code for this is shown below:
CGRect scrollviewFrame = CGRectMake(0, 30, 320, 380);
[scrollView setFrame:scrollviewFrame];
[self layoutScrollImages:YES];
CGSize srect = CGSizeMake([scrollView bounds].size.width, (kNumImages * (kScrollObjHeight + 10)));
[scrollView setContentSize:srect];
The constants mentioned in the above snippet are defined as:
const CGFloat kScrollObjHeight = 80;
const NSUInteger kNumImages = 100;
As I debug this project, I can see that srect is 320 (w) x 8000 (h) in size; however my issue is the scrollable area (where the user can actually touch to scroll the scrollview) remains the same as when it was it's original size.
I'm obviously missing something, does anyone know what it is?
have created a sample project to illustrate the issue I am having, it is available here: http://dl.dropbox.com/u/9930498/ScrollViewTest.zip
The problem in your sample is you have a very odd structure for loading your views. As such the view you're adding to the DetailScrollView instance is the root view of the DetailScrollView.xib, not the scrollview itself, which I believe is what you were expecting.
Fastest way to fix your problem is to adjust the root view in DetailScrollView.xib to autoresize width and height.
A UIView cannot respond to touches that are outside of the bounds of its superview. In your example, it appears that you expand the scroll view, but the scroll view's parent is still only 100 units high.
You should imagine the scrollView as a window, where by the window I mean the frame of the scrollView, which is also the coordinates that the scrollView detects your touches. By setting the contentView as 320 (w) x 8000 (h) you only change the content of the scroll view, which is the complete area behind that window.
By expanding content view, the scrollView can scroll a broader area, but in order to detect touches in a bigger rect, you should change frame of the scroll view.

Image scroller problem

hello every buddy
i want to make horizontal image scroller at bottom view and in the back of side big Image View. i don't know how to make image horizontal scroll.
Place the image view inside a scroll view whose horizontal content size is greater than its frame width, i.e.
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.frame = CGRectMake(0, 0, 100, 100);
scrollView.contentSize = CGSizeMake(300, 100);
[scrollView addSubview:imageView];
You should use contentSize property of the UIScrollView to set its content size according to your requirement and then set the property showsHorizontalScrollIndicator to YES and showsVerticalScrollIndicator to NO . But you don't require to set these two if you set frame and content size properly. Like if _myScrollView.frame.height and _myScrollView.contentSize.height is same then you don't need to set horizontal and vertical scroll. Its vertical scroll will automatically be disabled.
you can use scroll view with page control for a horizontal slideshow. follow the tutorial it might help you
http://www.edumobile.org/iphone/iphone-programming-tutorials/pagecontrol-example-in-iphone/