Questions tagged [cgrect]

CGRect is a structure from the ApplicationServices framework used with Apple's OSX and iOS operating systems. This structure is used to represent the origin and size of a rectangle.

CGRect is a structure that contains the location and dimensions of a rectangle.

Fields

  • origin: A point that specifies the coordinates of the rectangle’s origin.
  • size: A size that specifies the height and width of the rectangle.
716 questions
160
votes
8 answers

iOS: verify if a point is inside a rect

Is there a way to verify if a CGPoint is inside a specific CGRect? An example would be: I'm dragging a UIImageView and I want to verify if its central point CGPoint is inside another UIImageView.
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241
118
votes
8 answers

Get position of UIView in respect to its superview's superview

I have a UIView, in which I have arranged UIButtons. I want to find the positions of those UIButtons. I am aware that buttons.frame will give me the positions, but it will give me positions only with respect to its immediate superview. Is there is…
Shailesh
  • 3,072
  • 3
  • 24
  • 33
99
votes
4 answers

Comparing two CGRects

I needed to check wether the frame of my view is equal to a given CGRect. I tried doing that like this: CGRect rect = CGRectMake(20, 20, 20, 20); if (self.view.frame == rect) { // do some stuff } However, I got an error saying Invalid operands…
Tim Vermeulen
  • 12,352
  • 9
  • 44
  • 63
73
votes
13 answers

Simple way to change the position of UIView?

I change the position of a UIView with following codes without changing size of the view. CGRect f = aView.frame; f.origin.x = 100; // new x f.origin.y = 200; // new y aView.frame = f; Is there more simple way to change only the view position?
Seunghoon
  • 5,632
  • 5
  • 35
  • 41
47
votes
9 answers

iOS frame change one property (eg width)

This question was originally asked for the objective-c programming language. At the time of writing, swift didn't even exist yet. Question Is it possible to change only one property of a CGRect ? For example: self.frame.size.width = 50; instead…
Jacksonkr
  • 31,583
  • 39
  • 180
  • 284
45
votes
4 answers

Can I adjust a CGRect with a UIEdgeInsets?

I've got a CGRect, and I'd like to adjust it with a UIEdgeInsets. It seems like perhaps there might be a built in function that does this. I've looked for a CGRectAdjustByInsets or functions with some other CGRect… prefix, but I didn't find…
Benjohn
  • 13,228
  • 9
  • 65
  • 127
43
votes
5 answers

"Expression is not assignable" -- Problem assigning float as sum of two other floats in Xcode?

In a piano app, I'm assigning the coordinates of the black keys. Here is the line of code causing the error. 'blackKey' and 'whiteKey' are both customViews blackKey.center.x = (whiteKey.frame.origin.x + whiteKey.frame.size.width);
Mahir
  • 1,684
  • 5
  • 31
  • 59
39
votes
13 answers

Swift 4 Expression type '@value CGRect' is ambiguous without more context

I am trying to implement a animation for a rectangle to perform modal vertical swipe.However, when I try to compile the code I get the following error "Swift 4 Expression type '@value CGRect' is ambiguous without more context". I have isolated the…
CoderQueue
  • 498
  • 1
  • 4
  • 6
32
votes
4 answers

Border around UITextView

How do you add a border around a UITextView (like a UITextField)? I want to allow users to type multiple lines, but unfortunately a UITextField does not support this. Is there an easy way to do this in code or do I need to create one in Photoshop?
Jack
  • 361
  • 1
  • 4
  • 6
30
votes
2 answers

CGRectGetWidth vs CGRect.size.width

Which is better to use? I prefer CGRect.size.width cause it looks nicer. But, my colleague says CGRectGetWidth is better.
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
29
votes
1 answer

CGRectZero vs CGRect.zeroRect?

Both seem to work the same in Swift. Which should we use? CGRectZero vs CGRect.zeroRect GRectMake(1, 1, 1, 1) vs CGRect(x: 1, y: 1, width: 1, height: 1) UIEdgeInsetsMake(1, 1, 1, 1) vs UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1) Other…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
26
votes
4 answers

Check if CGRect null in getter

I am trying to check if a CGRect is null in my property getter and if it is, load a value by default, however, in my init method, when I use the property, it returns zero for all values. Here is my getter: - (CGRect)frame { …
crizzwald
  • 1,037
  • 2
  • 14
  • 30
23
votes
7 answers

Turn two CGPoints into a CGRect

How can I, given two different CGPoints, turn them into an CGRect? Example: CGPoint p1 = CGPointMake(0,10); CGPoint p2 = CGPointMake(10,0); How can I turn this into a CGRect?
christo16
  • 4,843
  • 5
  • 42
  • 53
21
votes
10 answers

How to set UIView size to match parent without constraints programmatically

The problem sounds easy but it is making me crazy. I've created a white view in IB that's called iBag and by constraints it's size depends on screen size. Now I want create a new UIView programmatically and add as subview to iBag with same size and…
Seifolahi
  • 919
  • 3
  • 11
  • 26
21
votes
7 answers

How can I increase the size of a CGRect by a certain percent value?

How can I increase the size of a CGRect by a certain percent value? Should I use some form of CGRectInset to do it? Example: Assume I have a CGRect: {10, 10, 110, 110} I want to increase its size (retaining the same center point) by 20% to: {0, 0,…
brandonscript
  • 68,675
  • 32
  • 163
  • 220
1
2 3
47 48