简体   繁体   中英

iPhone — is it possible to inspect the frame of a UIView in the Xcode debugger?

When the debugger is stopped at a breakpoint, I can't find the frame of any of my UIViews in there.

Is it possible to do this?

EDIT: starting a bounty due to the lack of response. Just to be clear, what I am looking for is a way to see the frame without adding in extra debugging code.

Also, if the answer is "no you can't do it", bounty will go to the best explanation of why you can see some class members but not others.

Yes, you can do it. While debugging, find the UIView of interest in the variable inspector. Control-click on it and select "Print Description to Console". For example, I did this on the _view ivar of a UIViewController and the following appeared in the console:

Printing description of _view:
<UIView: 0x25b460; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x26b740>>

If you go to the debugger panel, you can type this in while you are at a breakpoint:

(gdb) print (CGRect) [self frame]
$1 = {
  origin = {
    x = 0, 
    y = 0
  }, 
  size = {
    width = 100, 
    height = 100
  }
}

When using the console debugger you can press the up arrow key to cycle through previous commands. Pressing return without entering a command repeats the last command.

Re-formatting @EPage_Ed's answer because the original was hard-coded for his specific case:

At the (lldb) prompt, type:

print (CGRect)[view frame]

Or, for the bounds:

print (CGRect)[view bounds]
NSLog(@"My view frame: %@", NSStringFromCGRect(myView.frame));

In XCode 5.1.1, you can hover over a variable that is a UIView and you will see the following type of popover:

在此处输入图片说明

If you click on the 'i' button, the following type of output will be printed in the debugger's console:

<UIImageView: 0xa49ca90; frame = (0 0; 640 360); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xa46c1c0>>

This is another way of inspecting the frame of a UIView.

po [[[[UIApplication sharedApplication]windows] objectAtIndex:0] recursiveDescription]

将打印出整个视图,但是似乎只在gdb中起作用,而在llvm中不起作用

Interestingly, using the getter method to return the view's frame works:

print (CGRect)[view frame]

This gives the expected result:

(CGRect) $2 = origin=(x=0, y=20) size=(width=320, height=48)

But if you try to use dot notation, which I hear so often referred to as being provided simply for 'syntactic sugar':

print (CGRect)view.frame

You get the following error:

error: C-style cast from '<unknown type>' to 'CGRect' is not allowed

In Xcode, go to the console and type:

po viewName

If execution is inside code for the view, you can just:

po self

This will output some view details, like this:

<UIView: 0x9cca0f0; frame = (0 0; 320 480); layer = <CALayer: 0x9ccabe0>>

Found an answer for lldb. For example, this works

(lldb) print (CGRect)[((UIView *)[[[self backIV] subviews] objectAtIndex:1]) frame]

I prefer the short form for print ie 'p' to print the frame in lldb. For eg

p (CGRect)[view frame]

要获得类似于调试器的帧信息作为NSString,请使用NSStringFromCGRect(someView.frame)

Sometimes they are just out of scope by the time you get there.

Print them to the console:

NSLog('Frame: %d, %d, %d, %d', frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM