iPhone position of a subview? - iphone

I add a view dynamically, but when it appears on the form, it's in the upper left hand corner.
Where do I set the X and Y of the new subview?

You should set the frame property:
myView.frame = CGRectMake(10,10,200,100);
This will position the view at location (10,10), with a size of 200x100

Agreed.
Note that you cannot change coordinates individually, as you might expect. That is, this doesn't work:
self.mysubview.frame.origin.x += 17; // FAILS! Not assignable!!
If it was a pain to calculate all the other coordinates you need, you can (a) suck it up or (b) do something like the following:
CGRect theFrame = self.mysubview.frame;
theFrame.origin.x += 17;
self.mysubview.frame = theFrame; // this is legal

Related

UISegmentedControl bounds

I want to give the following aspect to an UISegmentedControl:
Note the gray background view, and the white background of the segmented control non selected item.
But, if I give a white background to my UISegmentedControl, I get the following:
Note the white square corners around the UISegmentedControl. What should I do to avoid that square corners?
Thank you in advance,
EDIT: If I change the corner radius of the UISegmentedControl's layer, as suggested by onegray, the result is better, but not perfect (note the white line at the right):
Setting the _segmentedControl.layer.cornerRadius = 5; might help.
Update: More complex clip rect to get rid of 1px right space:
CAShapeLayer* mask = [[CAShapeLayer alloc] init];
mask.frame = CGRectMake(0, 0, _segmentedControl.bounds.size.width-1, _segmentedControl.bounds.size.height);
mask.path = [[UIBezierPath bezierPathWithRoundedRect:mask.frame cornerRadius:4] CGPath];
_segmentedControl.layer.mask = mask;
Update: Matthias Bauch provided a good explanation why this whitespace appears on the right side of the UISegmentedControl. So the simplest way to remove it is making segments of fixed size and adjusting them for proper width.
If that should work for all UISegmentedControls it's a bit of a hassle.
The problem is in iOS7 the 1 pt. border between two segments does not count to the size of the segment. E.g. if the frame of your UISegmentedControl is 320 pt. wide you have to remove 1 pt. and than divide by 2.
And (320-1)/2 is 159.5. iOS floors this value down to 159 pt. And you end up with a 1 pt. border and two 159 pt. segments. Which is 319, and not 320. Hence the 1pt. line at the right of your segmentedControl.
There is a way to calculate the "actual" (the size of the rendering on screen) size of the segmentedControl. With that width you can then add a UIView with rounded corners below the UISegmentedControl.
This code should work for all configurations, even if you have manually sized segments in your segmentedControl:
- (UIView *)addBackgroundViewBelowSegmentedControl:(UISegmentedControl *)segmentedControl {
CGFloat autosizedWidth = CGRectGetWidth(segmentedControl.bounds);
autosizedWidth -= (segmentedControl.numberOfSegments - 1); // ignore the 1pt. borders between segments
NSInteger numberOfAutosizedSegmentes = 0;
NSMutableArray *segmentWidths = [NSMutableArray arrayWithCapacity:segmentedControl.numberOfSegments];
for (NSInteger i = 0; i < segmentedControl.numberOfSegments; i++) {
CGFloat width = [segmentedControl widthForSegmentAtIndex:i];
if (width == 0.0f) {
// auto sized
numberOfAutosizedSegmentes++;
[segmentWidths addObject:[NSNull null]];
}
else {
// manually sized
autosizedWidth -= width;
[segmentWidths addObject:#(width)];
}
}
CGFloat autoWidth = floorf(autosizedWidth/(float)numberOfAutosizedSegmentes);
CGFloat realWidth = (segmentedControl.numberOfSegments-1); // add all the 1pt. borders between the segments
for (NSInteger i = 0; i < [segmentWidths count]; i++) {
id width = segmentWidths[i];
if (width == [NSNull null]) {
realWidth += autoWidth;
}
else {
realWidth += [width floatValue];
}
}
CGRect whiteViewFrame = segmentedControl.frame;
whiteViewFrame.size.width = realWidth;
UIView *whiteView = [[UIView alloc] initWithFrame:whiteViewFrame];
whiteView.backgroundColor = [UIColor whiteColor];
whiteView.layer.cornerRadius = 5.0f;
[self.view insertSubview:whiteView belowSubview:segmentedControl];
return whiteView;
}
Please take care of frame changes yourself.
See this screenshot to see the difference between the two controls. All frames are 280 pt. wide.
Because of the formula UISegmentedControl uses the first controls actual size is 278 pt. And the real size of the second one is 279 pt.
The problem is that this somehow relies on the implementation of UISegmentedControl. Apple could for example change the implementation so segmentWidth that end in .5 points will be displayed. They could easily do this on retina displays.
If you use this code you should check your app on new iOS versions as early as possible. We are relying on implementation details, and those could change every day. Fortunately nothing bad happens if they change the implementation. It will just not look good.
I know this is kind of a hack but you could just use a rounded UIView with white background placed just underneath - and aligned with - the segmented control, except for the width which should be equal to the original control's width minus 1.
Result:
Just to clarify Mattias Bauch's excellent answer. You need to set the returned view as a subview to the view (which we call yourMainView) where you have your segmented control:
UIView *segmControlBackground = [self addBackgroundViewBelowSegmentedControl:yourSegmentedControl];
[yourMainView addSubview:segmControlBackground];
And you need to, of course, declare the new method in your header (.h) file:
- (UIView *)addBackgroundViewBelowSegmentedControl:(UISegmentedControl *)segmentedControl;

Associate CGAffineTransformRotation to CGAffineTransformTranslation

I am trying to rotate a custom view (VIEWA) added on a parent view based on touch gesture using CGAffineTransformRotate. Everything is working fine. Now, I have another view(VIEWB) added on the parent view which should follow the path traced by a corner of the VIEWA while being rotated.
What i did was to calculate the new coordinates of the VIEWB from the VIEWA transformation matrix and translated the subview. i.e
VIEWA.transform = CGAffineTransformRotate(startTransform, -angleDifference+M_PI_2);
CGFloat cosa = VIEWA.transform.a;
CGFloat msinb = VIEWA.transform.b;
CGFloat sinc = VIEWA.transform.c;
CGFloat cosd = VIEWA.transform.d;
CGFloat newX = VIEWB.center.x * cosa + VIEWB.center.y * msinb;
CGFloat newY = VIEWB.center.x * sinc + VIEWB.center.y * cosd;
CGFloat xdiff = newX - VIEWB.center.x;
CGFloat ydiff = newY - VIEWB.center.y;
VIEWB.transform = CGAffineTransformTranslate(VIEWB.transform, xdiff, ydiff);
But i could not get what i wanted. Can somebody help me ?
Update:
This is what I'm trying to do :(red dot is A and black popup is B):
I assume you rotate view A but the red circle stays at a fixed position in view A's coordinate system. Thus the center of the red circle, in view A's coordinate system, should be something like easy to compute, like this:
CGPoint redCircleCenterInViewA = CGPointMake(CGRectGetMidX(viewA.bounds), 12);
You can simply convert that point to the parent view's coordinate system, like this:
CGPoint redCircleCenterInParentView = [viewA convertPoint:redCircleCenterInViewA
toView:viewA.superview];
Then you can set view B's center to that point, minus half of view B's height:
viewB.center = CGPointMake(redCircleCenterInParentView.x,
redCircleCenterInParentView.y - viewB.frame.size.height / 2);
UPDATE
I see that you actually have layer A, not view A. That only requires a slight modification. The message you send to a layer to convert coordinates is slightly different than the message you send to a view.
CALayer *layerA = ...;
UIView *parentView = ...;
UIView *viewB = ...;
CGPoint redCircleCenterInLayerA = CGPointMake(CGRectGetMidX(layerA.bounds), 12);
CGPoint redCircleCenterInParentView = [layerA convertPoint:redCircleCenterInLayerA
toLayer:parentView.layer];
viewB.center = CGPointMake(redCircleCenterInParentView.x,
redCircleCenterInParentView.y - viewB.frame.size.height / 2);
Note that there are also convertPoint:fromLayer: and convertPoint:fromView: messages. Don't let Xcode autocomplete the wrong one or you will be scratching your head!

Setting TextLabel Position Objective-c

I have a Scroll View with many objects in it,since i cant edit the position on scrollview in InterfaceBuilder,how i set the position of the object programmactily?
Something like:
myTextField.x = 50;
myTextField.y = 540;
Use the frame property to set it relative to the superview's coordinates like this:
myTextLabel.frame = CGRectMake(x, y, width, height);
There is no textlabel but I assume you mean either UITextField or UILabel, either way both inherit from UIView and have the frame property.
You could also move it according to the object's center.
float x = 220; //Change This Number
float y = 180; //Change This Number
myTextField.center = CGPointMake(x,y);

UIView's position

I have 4 views and i am drawing circles inside these views.The user is able to move these views.How can i get the position of each view relative to the window(i mean relative to 320*480)?
I want to draw and fill a polygon using the position of views.
You can use the frame property of the UIView to retrieve its location and size. See the class reference for more information:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html
Example:
... = myView.frame.origin.x; //x-coord
... = myView.frame.origin.y; //y-coord
... = myView.frame.size.width; //width
... = myView.frame.size.height; //height
You can grab the position in the following way:
CGPoint positionOfAView = view.frame.origin;
or if transforms were applied:
CGPoint positionOFAView = view.bounds.origin;
Alternatively, you may want to grab the center:
CGPoint centerOfAView = view.center;
See this answer, too.
You have posted the same question again.(your first question) Before anybody answer's this question the same way it was answered earlier. Can you please tell,
Are you drawing the Circles in the view using CoreGraphics?
When you say 4 different views, how they are managed? Are they displayed at the same time?
Are there 4 View objects added on 1 ViewController?
You can get the position using
CGRect frame = [myView frame];
But remember it will send coordinates and origin based on its parent view
And after making changes
myView.frame = frame;
Hope this helps....
If you want to just move a view (without changing it size), consider using the center property, it might be more convenient.
Here's an example, assuming your circle structure is built in a certain way :)
myView.center = CGPointMake(circle.x + circle.width/2, circle.y + circle.height/2);

Rotate using a transform, then change frame origin, and view expands?

This is quite the iPhone quandry. I am working on a library, but have narrowed down my problem to very simple code. What this code does is create a 50x50 view, applies a rotation transform of a few degrees, then shifts the frame down a few times. The result is the 50x50 view is now much larger looking.
Here's the code:
// a simple 50x50 view
UIView *redThing = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
redThing.backgroundColor = [UIColor redColor];
[self.view addSubview:redThing];
// rotate a small amount (as long as it's not 90 or 180, etc.)
redThing.transform = CGAffineTransformRotate(redThing.transform, 0.1234);
// move the view down 2 pixels
CGRect newFrame = CGRectMake(redThing.frame.origin.x, redThing.frame.origin.y + 2, redThing.frame.size.width, redThing.frame.size.height);
redThing.frame = newFrame;
// move the view down another 2 pixels
newFrame = CGRectMake(redThing.frame.origin.x, redThing.frame.origin.y + 2, redThing.frame.size.width, redThing.frame.size.height);
redThing.frame = newFrame;
// move the view down another 2 pixels
newFrame = CGRectMake(redThing.frame.origin.x, redThing.frame.origin.y + 2, redThing.frame.size.width, redThing.frame.size.height);
redThing.frame = newFrame;
// move the view down another 2 pixels
newFrame = CGRectMake(redThing.frame.origin.x, redThing.frame.origin.y + 2, redThing.frame.size.width, redThing.frame.size.height);
redThing.frame = newFrame;
So, what the heck is going on? Now, if I move the view by applying a translation transform, it works just fine. But that's not what I want to do and this should work anyway.
Any ideas?
From the UIView documentation:
If the transform property is also set, use the bounds and center properties instead; otherwise, animating changes to the frame property does not correctly reflect the actual location of the view.
Warning: If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.
In other words, I would be wary of the frame property when a transform is set.