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.
Related
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 !
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 want to set a position of an object programmatically. For this I do:
youtubeIco.frame.origin.y = 100
but nothing. It doesn't change coordinates at all. What is the problem?
And a second question:
when I set in the XCode constraints of webView(for ex: 10(top, left, right, bottom) it disappears. But when I do not set constraints for it, it appears. What is the problem?
I want like this:
You need to set full frame like,
youtubeIco.frame = CGRectMake(youtubeIco.frame.origin.x, 100,youtubeIco.frame.size.width,youtubeIco.frame.size.height)
Only y-axis change will not do anything to it, you have to provide full frame for that. or set centre, or offset.
With AutoLayout you can't change a view's frame directly. The constraint puts the object right back where it was.
Instead, you should add constraints that position your views and add IBOutlets to those constraints. Then you use the outlet(s) to change the constant value of the constraint, and the constraint moves your view objects for you. (In this context anything you can display on the screen is a view {a descendant of UIView} including buttons.)
I have a setup like this in the interface builder:
(iphone5 size). I want the buttons to stay in that layout if the screen size increases i.e widens (but the buttons stay the same size). If I pin the top two buttons to their respective container edges when the screen is larger they will be too far apart from each other. I have the bottom button increasing its size fine. How can I do this? I tried putting a transparent UIView between each button and the container edge, but couldn't get it working this way. Could someone give me a pointer on how to do this please? thanks!
Assuming you already have the width, height and y-constraints in place, to create the correct x-constraints you can try something along the lines of this:
check the Horizontal Center in Container option
choose your button and double click the newly created constraint Align Center X to
change the Second Item from Center X to Trailing
enter a constant value to offset the position
Do this for both buttons. The constant value of the second button should be the negative of the first one AND its Second Item should be Leading.
Result
This method will result in equal spacing on all size classes.
The only drawback is that the spacing is no dynamic. for that purpose you would need some placeholder view in between.
I would say there are two approaches you could take:
Assumption is buttons have constraints for width and height.
1) Use a transparent view which you center horizontally within its containing view. Then pin the left buttons trailing edge to the transparent view and the right buttons leading edge to the center. You have a choice as to how to define their vertical position.
or
2) Add a transparent container view to your main view and then move your two buttons inside this view. Pin the two buttons apart the desired width and then pin the other button edges to the containing view at size 0pts. Finally center the new containing view horizontally in the view. You will need to define the height of this container view from the top or bottom.
The second is probably easier to execute within storyboard.
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.