How to change position (x,y coordinate) of a view dynamically - objective-c

I have an application which loads a view in it. I want to position the loaded view in 450pt of y cordinate. How to do that.
Regards
Ranjan

Look at the documentation for UIView and in particular the properties frame, bounds and center
I assume that you are adding a subview so you want it in the coordinate that is relative to the parent view. Then you use frame.
CGRect r = [subView frame];
r.origin.y = 450.0f;
[subView setFrame:r];
Something like that.

Related

How to dynamically add and remove views between two Views

I'm working on a cocoa app, in which i wanted to dynamically add, remove, resize views.
Shown above is the image, in which there are three views in a parent view out of which view-B can be added or removed and based on that we need to resize view-C.
Any cocoa/objective-c help for this.
//To add a subview
[parentView addSubview:subview];
//To remove a subview
[subview removeFromSuperview];
//To resize a view
[subview setFrameSize: NSMakeSize(width, height)];
[subview setFrameOrigin: NSMakePoint(originX, originY)];
If you need to set the frame of a view based on the size or position of other views, you can use a views frame which has a size and an origin etc...
For example, to move C so it borders A and takes up the rest of the space in the view:
NSSize size = NSMakeSize(parentView.frame.size.width, parentView.frame.size.height - a.frame.size.height);
NSPoint origin = NSMakePoint(a.frame.origin.x, a.frame.origin.y + a.frame.size.height);
[c setFrameSize: size];
[c setFrameOrigin: origin];

Redraw objects on another UIView

I have two circle objects on main view with coordinates: first.frame 100,100,20,20 and second 200,200,20,20
Main view frame is 0,0,320,480
I want to create another view with frame 50,50,200,200 and add my circles to that view, but that the new position was the same in relation to the main view
If I use this code I have:
...
[self.view addSubview:anotherView];
for (CircleView *circle in self.view.subviews)
{
if ([circle isKindOfClass:[CircleView class]])
[anotherView addSubview:circle];
}
My circles is replace to another view, but coordinates is same like a left-up corner not main view, another view
How to replace my objects to another view with same coordinates
You can use convertPoint:fromView: or convertPoint:toView: something like this:
CGRect newCircleFrame = circle.frame;
newCircleFrame.origin = [self.view convertPoint: circle.frame.orign
toView: anotherView];

UITextView not resizing correctly

I have a view that has a table view on the top, and a scroll view below the table view.
When I press the resize bar button item, I want to hide the table view and maximize the scroll view. I got the scroll view and table view to animate correctly, but I am trying to resize the UITextView inside the scroll view to take advantage of the extra screen space.
Whenever I calculate the resize, the UITextView goes to the top left corner of the screen, and I'm not sure why. I am not even modifying the X and Y, just the height.
CGRect newDesFrame = descriptionTextView.bounds;
newDesFrame.size.height = newDesFrame.size.height + tableViewFrame.size.height;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.5];
self.scrollView.frame = scrollFrame;
self.descriptionTextView.frame = newDesFrame;
[UIView commitAnimations];
I am not sure why this happens. Does the descriptionTextView.bounds get messed up since it's in a UIView inside a UIScrollView? It seemed that, when I do a NSLog of the X and Y of the scroll view, it's 0,0. It's weird since it's not at 0,0 in the superview, or in the view. How do I fix this?
descriptionTextView is most likely jumping to the top left because you are, in fact, changing the origin (x and y). You are starting with:
CGRect newDesFrame = descriptionTextView.bounds;
Getting the bounds of that text view will give you a CGRect with an origin of 0,0, as 'bounds' gives you the view's rectangle in its own, local coordinate space.
Try this instead:
CGRect newDesFrame = descriptionTextView.frame;
This will give you the view's rectangle in its superview's coordinate space, including the actual origin.
It happens because you are doing
self.descriptionTextView.frame=newDesFrame ( which is Bounds property, not frame!! To read about differences Link)
Solution:
self.descriptionTextView.bounds=newDesFrame

Set view position to top right programmatically in cocoa touch

I need to make sure that one view A (size: 200x200) is always aligned to top right corner inside second view B (full screen size). I want to make sure that view A stays in that place regardless of device orientation. Truth is I have no problem with this when using interface builder to position the views but I need to construct this programmatically. I suppose I should use some autoresizing settings, could you tell me which one is supposed to align the view to top right corner of its superview?
UIView parentView //your full screen view
UIView view //the 200x200 view
[parentView addSubview:view];
CGRect frame = view.frame;
//align on top right
CGFloat xPosition = CGRectGetWidth(parentView.frame) - CGRectGetWidth(frame);
frame.origin = CGPointMake(ceil(xPosition), 0.0);
view.frame = frame;
//autoresizing so it stays at top right (flexible left and flexible bottom margin)
view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin
That positions the view on top right and set's the autoresizing mask so it stays at that position.

location of a button/textfield in a view in cocoa

I'm sorry to ask such a question, but I can't seem to find the answer for it from the wild web.
I want to find out the location of a NSTextField in my custom view, so I can add other textfields under it programmatically.
The reason I don't just add other textfields in IB, is because I need to be able to dynamically create them.
The NSTextField is linked to an IBOutlet.
So the main question is: How do I find out the location of a NSTextField (or NSButton, it doesn't really matter atm) in a custom view (I need to get the coordinates of the item)?
Make an instance Variable with the NSTextField and then call these methods on it to get the four values you want.
NSRect textFieldFrame = [textField frame];
NSPoint textFieldLocation = textFieldFrame.origin;
NSSize textFieldSize = textFieldFrame.size;
NSInteger x = textFieldLocation.x;
NSInteger y = textFieldLocation.y;
NSInteger width = textFieldSize.width;
NSInteger height = textFieldSize.height;
Every kind of view or control (like NSButton, NSTextField, etc.) that inherits from NSView has a -frame method that contains the positioning information.
If your custom view has an IBOutlet connected up to the NSTextField, you could do the following:
NSRect textFieldFrame = [textField frame];
That NSRect contains all of the information for where the text field is positioned.
See the following 2 guides for more information:
View Programming Guide: Understanding a View's Frame and Bounds
Cocoa Drawing Guide: Coordinate Systems and Transforms
Any view has both a bounds and a frame property. bounds is the rectangle that encloses the view in that view's own coordinate space; frame is the enclosing rectangle in the superview's coordinate space. In this case, you want the text view's frame, i.e. its location and size in the view that contains it, so that you can locate other text views in the same superview.