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);
Related
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;
I am setting rotation and flip horizontally of UIImageView by using
#define DegreesToRadians(x) ((x) * M_PI / 180.0)
imageView.transform = CGAffineTransformConcat(CGAffineTransformMakeScale(-1, 1),CGAffineTransformMakeRotation(DegreesToRadians(-5)));
This operation changes xOrigin + yOrigin + width + height of UIImageView.
Can I maintain or reset new origin to actual origins of UIImageView Or Cann't I just rotate the UIImageView, without getting changes in frame.
You can store your origin to a CGPoint variable before applying transform. After that assign it to your image view.
CGPoint origin = imageView.frame.origin;
imageView.transform = CGAffineTransformConcat(CGAffineTransformMakeScale(-1, 1),CGAffineTransformMakeRotation(DegreesToRadians(-5)));
imageView.frame = CGRectMake(origin.x, origin.y, imageView.frame.size.width, imageView.frame.size.height);
// or
CGRect frame = imageView.frame;
frame.origin = origin;
imageView.frame = frame;
UPDATE
If you want to undo your rotation and want to go back to your previous look than use:
imvCoverPhoto.transform = CGAffineTransformConcat(CGAffineTransformMakeScale(-1, 1),CGAffineTransformMakeRotation(DegreesToRadians(0)));
Just need to pass the 0 in DegreesToRadians. This will transform your image back to original.
Hope this helps and exactly what you are looking for :)
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;
I have implemented a PDF viewer in my app. It's a scroll view which uses CATiledLayer for zooming.
A simpler approach would have been to use QLPreviewController, which supports PDF viewing with page control and zooming but I need fine control over the PDF page.
I have the viewer working but would like the redraw to kick in sooner. At the moment it only redraws the text when I zoom in to double the size (200%). So the text appears blurry until you zoom in far enough.
I have tried various combinations of levelOfDetail and levelOfDetailBias to try and get the re-drawing to occur sooner but to no avail.
Here is the code which creates the CATiledLayers:
//get the pdf crop box rectangle
CGPDFPageRef pageRef=[self.pdf getPdfPage:self.myPageNo];
CGRect cropBox = CGRectIntegral(CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox));
//scale to size of the screen
CGRect targetRect = self.view.bounds;
CGFloat xScale = targetRect.size.width / cropBox.size.width;
CGFloat yScale = targetRect.size.height / cropBox.size.height;
self.scale = xScale < yScale ? xScale : yScale;
//alway fill height of screen
self.scale = yScale;
//used to center the pdf horizontally
self.xOffSet = (targetRect.size.width - cropBox.size.width*self.scale)/2;
[self renderPDFPageToImage];
CGRect boundsRect = self.view.bounds;
//use a tiled layer for better performance
CATiledLayer *tiledLayer = [CATiledLayer layer];
tiledLayer.delegate = self;
tiledLayer.tileSize = CGSizeMake(1024.0, 1024.0);
tiledLayer.levelsOfDetail = 4;
tiledLayer.levelsOfDetailBias = 4;
tiledLayer.frame = boundsRect;
self.myContentView = [[[UIView alloc] initWithFrame:boundsRect] autorelease];
[self.myContentView.layer addSublayer:tiledLayer];
CGRect viewFrame = self.view.frame;
viewFrame.origin = CGPointZero;
UIScrollView *_scrollView = [[UIScrollView alloc] initWithFrame:viewFrame];
_scrollView.delegate = self;
_scrollView.contentSize = boundsRect.size;
_scrollView.maximumZoomScale = 4;
[_scrollView addSubview:myContentView];
//retain it
self.scrollView=_scrollView;
[self.view addSubview:_scrollView];
[_scrollView release];
Please point me in the right direction.
I have a UIImageView whose frame, set before the image is loaded, is always too large for the image, so when I try to round the corners, for example, nothing happens.
How can I resize the frame so it's the same size as the underlying image, while ensuring that the center point of the UIImageView does not change?
If you change the bounds of a UIView the center will not change.
CGRect bounds;
bounds.origin = CGPointZero;
bounds.size = newImage.size;
imageView.bounds = bounds;
imageView.image = newImage;
try something along the following lines.. not tested
CGSize imageSize = image.Size;
CGPoint oldCenter = imageView.center;
// now do some calculations to calculate the new frame size
CGRect rect = CGRectMake(0, 0, imageSize.width, imageSize.height);
imageView.frame = rect;
imageView.center = oldCenter;