Changing UIView size programmatically - iphone

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;

Related

UITextView will not resize

I have tried resizing a UITextView (inside a tableViewCell) to the content size. but it will not change its height at all. I have even changed the height of the UITableViewCell. What could be wrong?
- (void) setTextViewContents: (NSString*) string
{
[textView setText:string];
CGRect frame2 = self.frame;
frame2.size.height = 10000;
self.frame = frame2;
/* resize the view */
CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height+60;
textView.frame = frame;
The string does appear on the view but the size does not change.
Try calling the -[UIView sizeToFit] method.

How to move CGRect in a custom cell?

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

Scale view to center subview

I've got a problem which I can't seem to solve.
I'd like to scale my application's view so that it appears that a view has 'zoomed in' so that it maintains its aspect ratio and fills the entire width of the screen.
I (think) I have some math to make this work, but I'm not sure how to apply this in a CGAffineTransform statement, or how to center the view.
Step 1:
Scale the view:
float scaleFactor = (320 / boxWidth);
self.view.transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
Step 2:
Position the view so it appears it's centered:
CGRect newFrame = self.view.frame;
newFrame.x = 20;
newFrame.y = mainBox.frame.origin.y;
self.view.frame = newFrame;
And this is where I'm stuck. I'm not sure how to position the view so that it appears it's centered.
Here's an image to demonstrate what I'd like to achieve:
Bascially, I'd like the view to scale so that the black box animates into the position and size of the red box.
I'm a bit stuck on this, so any help is appreciated.
In such cases it's easier to just calculate the final frame of the view and set it inside an animation block:
CGRect newFrame = self.view.frame;
newFrame.size.height *= 320.0f / newFrame.size.width;
newFrame.size.width = 320.0f;
newFrame.origin.x = 0.0f;
// Set newFrame.origin.y as desired
[UIView animateWithDuration: 0.5 animations: ^{
self.view.frame = newFrame;
}];

UIScrollView height to adjust when width is adjusted

I have a UIScrollView and I wanted the frame height to adjust proportionally when I adjust the width, is this possible? Basically I am talking about auto adjusting the frame height of the UIScrollView when I adjust the width of the UIScrollView? I have tried setting the autoResizingMask to UIViewAutoresizingFlexibleWidth and height, but this doesn't work
I don't think there's an automatic way to do it. but you can added a UIView+SCaleAddition to do this:
#interface UIView(RespectScale)
- (void)setWidthRespect:(float)w;
- (void)setHeightRespect:(float)h;
#end
#implement UIView(RespectScale)
- (void)setWidthRespect:(float)w
{
float scale = self.bounds.size.width / self.bounds.size.height;
float h = w/scale;
CGRect bounds = self.bounds
bounds.size.width = w;
bounds.size.height = h;
self.bounds = bounds;
}
- (void)setHeightRespect:(float)h
{
// write it your own
}
#end
You can do:
//get old proportions
CGFloat proportion = scrollView.frame.size.width / scrollView.frame.size.height;
CGRect frame = scrollView.frame;
frame.size.width = new_width;
frame.size.height = new_width * proportion;
scrollView.frame = frame;

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);