How to move CGRect in a custom cell? - iphone

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

Related

Changing UIView size programmatically

I've a UIView, I want to change the size when user touches a button.
CGRect newFrame = self.myview.frame;
newFrame.size.width = 200;
newFrame.size.height = 200;
[self setFrame:newFrame];
CGRect newFrame = self.searchField.frame;
newFrame.size.width = 200;
newFrame.size.height = 200;
self.searchField.frame=newFrame;
None of them works, don't change anything. I want to set fixed width and height to UIView.
If I understand correctly, you want to change the size of self.myview. However at no point you are setting the frame of it. Instead you are trying to call sendFrame: on the view controller and some search field. I'm surprised, that the former one didn't give you a compiler error.
Objective C
CGRect newFrame = self.myview.frame;
newFrame.size.width = 200;
newFrame.size.height = 200;
[self.myview setFrame:newFrame];
Swift
var newFrame = myview.frame
newFrame.size.width = 200
newFrame.size.height = 200
myview.frame = newFrame
In my case, I had a constraint on the width of my view, so I couldn't change the width like Tim said.
What I've done : I created an outlet on my constraint, called myviewWidthConstraint for example. Then I used this outlet to change the width of my view like this :
mycell.myviewWidthConstraint.constant = newSize;
Size fields are read-only, just make a new one -
//Set height
let f = view.frame; //Grab current
view.frame = CGRect(x: f.origin.x, y: f.origin.y, width: f.width, height: 200);
view.frame = newframe;
or
view.center = newcenter;

Dynamic UITextView in a dynamic UIScrollView - iOS

I'm facing a problem which takes me too long to solve.
I built a view, which contains a ScrollView. in the ScrollView, there's a view, which contains an image, and a UITextView. the textview should be in a dynamic height, with scrolling disabled. the textview gets all the text, but cut it off, and shows only the text that fits the height. in addition, the ScrollView doesn't changes.
- (void)viewDidLoad
....
//sets the text to the textview
[self.contentArticle setText:[NSString stringWithString:xmlParser.articleContent]];
//configures the scrollView
[self configureScrollView];
....
- (void)configureScrollView {
[self.contentView addSubview:self.contentArticle];
[self.scrollView addSubview:self.contentView];
CGRect frame = self.contentView.frame;
frame.size.height = self.contentArticle.contentSize.height;
self.scrollView.frame = frame;
[self.contentView sizeToFit];
[self.scrollView sizeToFit];
self.scrollView.contentSize = self.contentView.frame.size;
self.contentArticle.editable=NO;
self.contentArticle.scrollEnabled=NO;
//enable zoomIn
self.scrollView.delegate=self;
self.scrollView.minimumZoomScale=1;
self.scrollView.maximumZoomScale=7;
I did so many changes, and im not sure what is going on in there anymore!...
help would be sooo nice :)
UPDATE-
- (void)configureScrollView {
[self.contentView addSubview:self.contentArticle];
[self.scrollView addSubview:self.contentView];
CGRect textViewFrame = self.contentArticle.frame;
textViewFrame.size = [self.contentArticle contentSize];
self.contentArticle.frame = textViewFrame;
[self.scrollView setContentSize:textViewFrame.size];
self.contentArticle.editable=NO;
self.contentArticle.scrollEnabled=NO;
}
Try
- (void)configureScrollView{
self.contentView.autoresizingMask = UIViewAutoresizingNone;
self.contentArticle.autoresizingMask = UIViewAutoresizingNone;
CGRect textViewFrame = self.contentArticle.frame;
textViewFrame.size = [self.contentArticle contentSize];
self.contentArticle.frame = textViewFrame;
CGRect contentViewFrame = self.contentView.frame;
contentViewFrame.size.height = textViewFrame.origin.y+textViewFrame.size.height;
self.contentView.frame = contentViewFrame;
[self.scrollView setContentSize:contentViewFrame.size];
self.contentArticle.editable=NO;
self.contentArticle.scrollEnabled=NO;
//enable zoomIn
self.scrollView.delegate=self;
self.scrollView.minimumZoomScale=1;
self.scrollView.maximumZoomScale=7;
}
Source code
had same issue , tried to find solution for many days and finally i got it working.
I have a textview inside a scrollview.
disabled autolayout from storyboards
ive added the textview to scrollview with addsubview
and finally set the content of the scrollview, to the textview frame height.
Below my code:
_textView.text = stripped; //(some string ,yours: contentArticle)
[_textView sizeToFit];
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.scrollEnabled = NO;
_textView.frame = frame;
[self.scrollView addSubview:_textView];
[self.scrollView setContentSize:CGSizeMake(320,frame.size.height)];
Hope it helps!
You need to set self.contentView height first Use this code:
- (void)configureScrollView {
[self.contentView addSubview:self.contentArticle];
CGRect frame = self.contentArticle.frame;
frame.size.height = self.contentArticle.contentSize.height;
self.contentArticle.frame = frame;
[self.scrollView addSubview:self.contentView];
frame = self.contentView.frame;
frame.size.height += self.contentArticle.frame.height;
self.contentView.frame = frame;
self.scrollView.contentSize = self.contentView.frame.size;
self.contentArticle.editable=NO;
self.contentArticle.scrollEnabled=NO;
//enable zoomIn
self.scrollView.delegate=self;
self.scrollView.minimumZoomScale=1;
self.scrollView.maximumZoomScale=7;
}

Find real frame of UIVIew

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;

Fitting View Frame around Scaled CALayer

In the following method, I'm simply enlarging a bunch of CAShapeLayers which are attached to my views layer property by using CATransform3DMakeScale. I'm able to enlarge the shape layers correctly, but am having a tough time getting the view frame to enlarge and fit around the graphic produced by the shape layers like it should. Everything is the same size, ie the view frame is the same size as all of the shape layers.
Anyone have any suggestions on how to fix this? I've tried altering the views transform property as well as the views layer transform property. Not sure what I'm missing here.
- (void) setSize:(CGSize)size
{
CATransform3D transform = CATransform3DMakeScale(size.width / (self.graphic.initialSize.width), size.height / (self.graphic.initialSize.height), 1);
self.layer.sublayerTransform = transform;
self.frame = CGRectMake(0, 0, size.width, size.height);
self.graphic.size = CGSizeMake(size.width, size.height);
}
Also for reference, here is how I am setting up the views layer:
- (id)initWithGraphic:(Graphic*)graphic
{
self = [super initWithFrame:CGRectZero];
if (self)
{
self.backgroundColor = [UIColor clearColor];
_graphic = [graphic retain];
self.frame = CGRectMake(0,0, _graphic.initialSize.width, _graphic.initialSize.height);
self.layer.shouldRasterize = YES;
for (int i = 0; i < [_graphic.shapeLayers count]; i++)
{
[self.layer addSublayer:[_graphic.shapeLayers objectAtIndex:i]];
}
}
return self;
}
and here is how I'm setting up one of the shape layers:
CAShapeLayer *outlineShapeLayer = [[CAShapeLayer alloc] init];
outlineShapeLayer.anchorPoint = CGPointMake(0, 0);
outlineShapeLayer.fillColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1].CGColor;
outlineShapeLayer.bounds = shapeLayerFrame;
outlineShapeLayer.mutablePath = mutablePath;
I had to make it so my views layer also had it's anchorPoint and position set up to match my CAShapeLayers and then it worked:
self.layer.anchorPoint = CGPointMake(0, 0);
self.layer.position = CGPointMake(0, 0);

How to modify the position of UIImageViews subviews inside a UIScrollView?

I'm trying to create gaps between the UIImageViews, which are subviews added into a UIScrollView.
And I thought I could do the following by modifying the CGRect of the UIImageView.
CGRect frame = imageView.frame;
frame.origin.x = <some values>
frame.origin.y = 0
imageView.frame = frame;
By modifying the x values, I assumed it will help me with the position of the UIImageViews inside the UIScrollView.
However, I realized whichever values I set for the frame.origin.x does not really matter. As each UIImageView inside the UIScrollView will be positioned side by side without any empty space.
I think you're doing it in the wrong way. You can't edit view's position by
view.frame.origin.x = something;
You should assign a new frame, like this:
view.frame = CGRectMake(x, y, w, h);
In your case it will look like this:
CGRect frame = imageView.frame;
CGFloat frameX = frame.origin.x;
CGFloat frameY = frame.origin.y;
CGFloat frameW = frame.size.width;
CGFloat frameH = frame.size.height;
frameX = <some values>
frameY = 0;
imageView.frame = CGRectMake(frameX, frameY, frameW, frameH);