I have a custom view inside a view controller. I'm trying to setting the CGRect of custom view from view controller
I have few more buttons in custom view and i want to position them based on frame's coordinates but in the ovveride draw method, it takes bounds of view.
I want all the view to be drawn using frame coordinates instead of bounds
Pls advice how to acheive that
When you instantiate a view programaticly, you should do the following :
1) define the size of the rectangle that will contain your view.
2) Find the origin of this rectangle (is set to be the upper left corner) in the cartesian system (x,y) of the parent view of the view that you are creating, as function of the width and the height of the parent View .
3) Set the frame of your view as myView.frame = CGRect(origin: myOrigin, size: mySize)
You should always use the 'frame' property to define a new view.
I usually work with custom view, containing themselves many other custom Views, custom Buttons, ... The best way to do so (especially when it's getting complicated), is to take a paper & and pencil, draw the (x,y) axis of the parent View (y points downwards and x points to the right) and find all coordinates of all the origins in terms of the width and the height of the view in which you are working.
When you are working with subclasses of UIView (or any other object) you can easily use the system of coordinate defined at the origin of the view that you are creating (in the draw method)
Hope this was helpful, you can publish your code if you want more specific help !
Related
I've got a stack view and I'm wanting when the user clicks on one of the buttons a little "bar" view centers underneath it (the view is outside of the stack view). I had this working before I layed out the buttons with autolayout by just setting
movableView.center.x = newView.center.x
and animating it. Now with the buttons in the stack view the movableView's center X does not line up with the buttons' center x's. It's like 1/3 way off centered.
The movable view itself has no autolayout on it.
What is the cause of this and how can I align the center x of views that have auto layout in play?
Without more information this is my best guess as to what the issue may be.
The center attribute is based on the view's frame, which means that it is using the superview (in this case the UIStackView) coordinate system. If your UIStackView's frame.origin.x is not at 0, which is how it appears in your screenshot, you will need to adjust your movableView.center.x accordingly. This could be as simple as:
movableView.center.x = newView.center.x + stackView.frame.origin.x
Another option would be to convert the center to the main view's coordinate system, like this:
movableView.center.x = newView.convert(newView.center, to: self.view).x
I draw a custom UIbutton. Now I want, that the 0,0-point is in the middle of the object. Not on top-left-corner. So If I set the position of this button in a view to the middle point of the view, the button is really in the middle and I dont have to add the half of the width and the half of the height.
Example:
If I set the position of the object to the middle of a view it should look this:
and not like this:
You're modifying the origin of the frame, each view has a property center which does exactly what you want.
I want to create a vertical UISlider and exactly fit it into an existing container view that is sized and placed from a .xib file using autoLayout. But I simply cannot get it to work and I have Googled my fingers bloody.
The closest I have gotten creates a slider that is too long and extends off the screen.
Am I putting the code in the wrong place?
I am putting my code in "layOutSubviews"
Some Points of interest:
1.Per Apple's docs, the view's "frame" is not valid after the transformation and should not be set.
2.The frame of the slider is set to the future parent's dimensions before the transformation (with x and y swapped).
3.The transformed view appears to maintain its bounds in the pre-transformation coordinate frame. i.e. After the 90 degree transformation, the width and height of the transformed view's bounds appear to be swapped.
This code doesn't work. The slider looks right except that it extends off the bottom of the screen. The slider bounds and even the frame (which Apple says is not valid) seem to match the bounds and frame of the container view. The slider doesn't even stay within its own bounds.
[super layoutSubviews];
CGRect trigLevelSliderFrame=self.trigLevelSliderContainer.bounds;
trigLevelSliderFrame.size.height=self.trigLevelSliderContainer.bounds.size.width;
trigLevelSliderFrame.size.width=self.trigLevelSliderContainer.bounds.size.height;
UISlider *mySlider=[[UISlider alloc] initWithFrame:trigLevelSliderFrame];
self.trigSlider=mySlider;
[mySlider release];
self.trigSlider.transform=CGAffineTransformMakeRotation(-M_PI_2);
CGPoint center=CGPointMake(self.trigLevelSliderContainer.bounds.size.width/2,self.trigLevelSliderContainer.bounds.size.height/2);
self.trigSlider.center=center;
[self.trigLevelSliderContainer addSubview:self.trigSlider];
There is no problem with this code to add and rotate the slider. The problem is that the the code must be put into the "layoutSubviews" method of the slider's parent view; instead I had it in the "layoutSubviews" of the parent view of the parent view of the slider. When my original code executed the slider's parent view had not yet been laid out and did not yet have the correct dimensions.
During "layoutSubviews" of a view, the subviews have not yet been laid out and so their bounds are not yet valid. I needed to wait until later in the layout process to get the bounds of the parent view and transform the slider to fit.
In the end, I put the code to add the slider and transform the slider in the "viewDidAppear" of the top level view controller. This was the same code as in the original question - just in a different place.
I am developing an iOS app and ran into a weird problem. I am doing a log of resizing of views and so on view did load, I have an ivar that holds the initial size of the view that I sized in the Interface Builder. The Initial CGFrame rect is: (x = 0, y = 63, width = 284, height = 705). The code I use to hold the initial value is:
filesFrame = self.fileView.frame;
and the only places i touch self.fileView.frame is:
self.fileView.frame = filesFrame;
self.fileView.frame = fullFilesFrame;
These are in two separate spots not right after each other
fullFilesFrame is defined as:
fullFilesFrame = CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y+63,
self.view.frame.size.height,self.view.frame.size.width+63);
When I check my values after I resize back to the initial frame, The frame rect is now origin CGPoint (x=0, y=63)
CGSize (width=28, height=961)
how can this be?
This is the first time I'm dealing with resizing of views, and Im fairly new to iOS as well.
I was going to put this in a comment, but it's too long, so:
One trick I like in these situations is to override setFrame: in the class of the view I'm having trouble with, and set a break point in that overridden method. If you do that then you can see when the frame changes and take a look at the call stack.
It's not always totally straight forward, overriding setFrame:. The frame property can be set directly but it can also be derived from the view's bounds and center, so you might need to look for things setting the view's bounds or the view's center instead of the frame.
Also if the view has a non-identity transform, that will make the frame property behave very strangely. If you are setting non-identity transforms on your view you should just avoid the frame property altogether.
I'm trying to zoom in and out an UIView, and rearrange it content to look similar for both states: zoomed and normal.
This picture shows the default state (the view that I'm going to zoom has orange color and has 5 UIImageViews) :
When I press "Zoom in" button I change orange view frame:
_page.frame = self.view.bounds;
And I'm getting the following result:
But the goal that I want to achieve is something similar to this (same result if I would scale the view):
It means that I must change frames for each subview, but it could be complicated when view would have many objects on it.
What I'm asking for are some hints or methods how can I get desired result without accessing subviews.
There are be hacks to do this, but the proper way would be to use auto layout. You don't have to access any subviews and will be able to do it in the storyboard/IB.
If you use auto layout, you can actually create constraints which will pin the following attributes of the subviews:
Pin the top subview's top space and leading space to the container
Pin the all but the last subviews' vertical distance to its nearest neighbour and leading space to container
Pin the last subview's top vertical space to its nearest neighbour and bottom space to container and leading space to container
Set constraints for height and width but set the priority to low
In addition to setting the frame (which just changes the size of the view) you want to change the transform (scale the view) Try something like:
_page.transform = CGAffineTransformMakeScale(2.0, 2.0)
You'll probably want to calculate the scale factor based on the old view size and the new size.