I have a custom UIView I create in my class. I then add it to my screen and I want to know it's CGRect. But from what it seems, I can't seem to get the size of this particular UIView. Here's my code:
CGRect labelFrame;
if (!tickerImage) {
labelFrame = CGRectMake(upperTextField.frame.origin.x + 8, upperTextField.frame.origin.y, 0, 0);
} else {
labelFrame = CGRectMake(upperTextField.frame.origin.x + 16, upperTextField.frame.origin.y, 0, 0);
}
SGNewLabel = [[SGAdressLabel alloc] initWithFrame:labelFrame];
[labelsArray addObject:SGNewLabel];
[self.view addSubview:SGNewLabel];
[SGNewLabel addNewLabelWithText:labelText andTickerImage:tickerImage];
UIView *lastLabel = [labelsArray lastObject];
CGRect newTextFieldFrame = CGRectMake(lastLabel.frame.origin.x + lastLabel.frame.size.width, labelFrame.origin.y, 320, 30);
upperTextField.frame = newTextFieldFrame;
As you see, I set the upperTextField frame based on the frame of my custom SGNewLabel which appears to be wrong, even if put into NSArray. I set 0, 0 at the end of the labelFrame declaration because I don't know the size of the future object and here is the source of my problem.
How can I correct it and get the right size of my UIView?
EDIT:
NSLog on my view gives me this:
NSLog(#"The rect of object is: %#", NSStringFromCGRect(self.frame));
2013-02-11 17:16:02.333 MyApp[2383:c07] The rect of object is: {{24, 55}, {0, 0}}
The UIView itself is drawn normally and I can see it full-sized.
I should have used my label's frame set in its own class and then my code would look like this:
SGNewLabel = [[SGAdressLabel alloc] init];
CGRect labelFrame;
if (!tickerImage) {
labelFrame = CGRectMake(upperTextField.frame.origin.x + 8, upperTextField.frame.origin.y, SGNewLabel.frame.size.width, SGNewLabel.frame.size.height);
} else {
labelFrame = CGRectMake(upperTextField.frame.origin.x + 16, upperTextField.frame.origin.y, SGNewLabel.frame.size.width, SGNewLabel.frame.size.height);
}
SGNewLabel.frame = labelFrame;
Related
So, I have a custom cell class and in the implementation I have this code:
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame = CGRectMake(boundsX+10,10, 50, 50);
picture.frame = frame;
frame = CGRectMake(boundsX+75 ,0, 170, 50);
nameLabel.frame = frame;
frame = CGRectMake(boundsX+75 ,43, 225, 23);
costLabel.frame = frame;
frame = CGRectMake(boundsX+280, 30, 8, 13);
arrow.frame = frame;
}
Now let's see how this layout works at the picture below:
I am satisfied with the result, but I also want to change costLabel position in the cell according to the nameLabel. If there are two lines of text in the nameLabel, I don't want to change the costLabel position, however if there are just one line of text in the nameLabel, I want to move costLabel upper, so I can rid off the space and make costLabel closer to nameLabel.
I tried this in the cellForRowAtIndexPath:
CGRect frame = cell.costLabel.frame;
frame.origin.y = frame.origin.y-10; // new y coordinate
cell.costLabel.frame = frame;
But it didn't work.
Any solutions or ideas, how can I change CGRect coordinates (y origin) ?
layoutSubviews is going to be called when it wants to draw your cell. Make your frame changes in layoutSubviews (i.e.
if (thereIsOnlyOneLineInThisCell) {
frame = CGRectMake(boundsX+75 ,43 -10, 225, 23);
} else {
frame = CGRectMake(boundsX+75 ,43, 225, 23);
}
costLabel.frame = frame;
You can calclulate the size of the nameLabel with : NSString UIKit additions
I've got a MKMap, on top of this I have a little legend. The labels & images inside the legend successfully show on top of the map, but the rectangle/bezier itself doesn't show.
I've tried adding it as a subview and that fails. Is there a way around this?
I need touch to still be enabled for the map.
Thanks.
- (void) drawRect:(CGRect)rect
{
//TODO
//Hover view for touching icons
//Get rect on top of map as well
//Border on frame
//IsIpad slight larger frame/text/image
int conX = [GeneralHelper GetCenteredXPosForObj:220 :self];
int conY = 60;
//Create box
CGRect container = CGRectMake(conX, conY, 220, 50);
UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:container cornerRadius:5.0];
[[UIColor blueColor] setFill];
[path fillWithBlendMode:kCGBlendModeNormal alpha:0.7];
//How to add to subview..?
//NZPost image 38x37
UIImageView *nzPostImageView = [[UIImageView alloc] initWithFrame:CGRectMake(conX + 20, conY+15, 25, 24)];
[nzPostImageView setImage:[UIImage imageNamed:#"icon-map-post.png"]];
[self addSubview:nzPostImageView];
//NZPost label
LegendLabel *postLabel = [[LegendLabel alloc] initWithFrame:CGRectMake(conX + 50, 78, conY, 15) ];
postLabel.text = #"NZ post";
[self addSubview: postLabel];
//Incharge image 38x38
UIImageView *inChargeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(conX + 120, conY+15, 25, 24)];
[inChargeImageView setImage:[UIImage imageNamed:#"icon-map-incharge.png"]];
[self addSubview:inChargeImageView];
//Incharge label
LegendLabel *inChargeLabel = [[LegendLabel alloc] initWithFrame:CGRectMake(conX + 150, conY+18, 60, 15) ];
inChargeLabel.text = #"In charge";
[self addSubview: inChargeLabel];
}
I have an if statement that checks if an array has a certain item:
if ([[subTitles objectAtIndex:indexPath.row] isEqualToString:#""]) {
and has this code inside it:
CGRect CellFrame = CGRectMake(0, 0, 300, 44);
Then I have an else statement with the same thing but with different values, obviously.
It looks like this all together:
if ([[subTitles objectAtIndex:indexPath.row] isEqualToString:#""]) {
CGRect CellFrame = CGRectMake(0, 0, 300, 44);
} else {
CGRect CellFrame = CGRectMake(0, 0, 300, 60);
}
Later in my code, I am trying to use CellFrame but it says it is undeclared. What is the proper way to make a CGRect if/else statement without having to put all of the code in each statement? Please say if I am unclear so I can provide more detail. Thanks.
You need the variable to be defined outside of the if/else statement. When you put it inside, it is only visible to code in that statement and will be destroyed on exit. Also, since only the height is different, you could assign the other parts of the rectangle outside of the if statement.
CGRect CellFrame;
CellFrame.origin = CGPointMake(0,0);
CellFrame.size.width = 300;
if([[subTitles objectAtIndex:indexPath.row] isEqualToString:#""]) {
CellFrame.size.height = 44;
} else {
CellFrame.size.height = 60;
}
i have following problem: i generate subviews UIView in an UIScrollView including UIImageViews.
NSArray *bilder = [[NSArray alloc] initWithObjects:#"lol.jpg", #"lol2.jpg", nil];
NSArray *added = [[NSMutableArray alloc] init];
UIImageView *tmpView;
for (NSString *name in bilder) {
UIImage *image = [UIImage imageNamed:name];
tmpView = [[UIImageView alloc] initWithImage:image];
tmpView.contentMode = UIViewContentModeScaleAspectFit;
tmpView.frame = CGRectMake(0, 0, 300, 300);
[added addObject:tmpView];
[tmpView release];
}
CGSize framy = mainFrame.frame.size;
NSUInteger x = 0;
for (UIView *view in added) {
[mainFrame addSubview:view];
view.frame = CGRectMake(framy.height * x++, 0, framy.height, framy.width);
}
mainFrame.scrollEnabled = YES;
mainFrame.pagingEnabled = YES;
mainFrame.contentSize = CGSizeMake(framy.height * [added count], framy.width);
mainFrame.backgroundColor = [UIColor blackColor];
i get image file names out of an array. now i want to resize the imageview to a specific size, but it won't work. any advice?
thanks + regards
UIImage lacks the setSize: method of NSImage, but this post seems to add a scaleToSize: method using a category on UIImage.
Change tmpView.frame to use a new CGRect:
tmpView.frame = CGRectMake(tmpView.frame.origin.x,
tmpView.frame.origin.y,
newWidth,
newHeight);
What I see is:
view.frame = CGRectMake(framy.height * x++, 0, framy.height, framy.width);
But if you look at CGRectMake...
Declaration: CGRect CGRectMake (
CGFloat x,
CGFloat y,
CGFloat width,
CGFloat height
);
You are setting the X value, not the Y value. I expect, based on your later call to:
mainFrame.contentSize = CGSizeMake(framy.height * [added count], framy.width);
that you intend to make the these images appear on top of each other, not side by side.
Suggest:
view.frame = CGRectMake(framy.height * x++, 0, framy.height, framy.width);
to
view.frame = CGRectMake(0,framy.height * x++, framy.height, framy.width);
I'm trying to pass a CGRect:
SEL frameSel = NSSelectorFromString(#"setFrame:");
CGRect rect = CGRectMake(10, 10, 200, 100);
[object performSelector:frameSel withObject:rect ];
But this does not compile
I also tried:
SEL frameSel = NSSelectorFromString(#"setFrame:");
CGRect rect = CGRectMake(10, 10, 200, 100);
NSValue * value = [NSValue valueWithCGRect:rect];
[object performSelector:frameSel withObject:value ];
Actually, this does compile but when I debug, the frame is not setted correctly:
po object
<UILabel: 0x39220f0; frame = (0 0; 200 100); text = 'reflectionLabel'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x3922240>>
But it should be frame = (10 10; 200 100)
How can I solve this problem?
Thank you in advance!
CGRect rect = CGRectMake(10, 10, 200, 100);
[object performSelector:frameSel withObject:rect ];
But this does not compile
Correct, because rect is a structure, not a pointer to an object.
CGRect rect = CGRectMake(10, 10, 200, 100);
NSValue * value = [NSValue valueWithCGRect:rect];
[object performSelector:frameSel withObject:value ];
Actually, this does compile but when I debug, the frame is not setted correctly:
po object
<UILabel: 0x39220f0; frame = (0 0; 200 100); text = 'reflectionLabel'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x3922240>>
Well, it's not garbage, so it would seem that it worked, even though the origin somehow remained zero. You may want to ask a separate question about that.
Anyway, why are you using performSelector:withObject:? You're not addressing it to a specific thread and you're not putting it on a delay; why not simply say object.frame = rect?
You must use an NSInvocation to handle any non-id arguments or return values:
CGRect rect = CGRectMake(104, 300, 105, 106);
UIView *view = [[UIView alloc] init];
NSMethodSignature *methodSig = [view methodSignatureForSelector:#selector(setFrame:)];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSig];
invocation.target = view;
invocation.selector = #selector(setFrame:);
[invocation setArgument:&rect atIndex:2];
[invocation invoke];