简体   繁体   English

获取CGRects的UIView类的尺寸?

[英]Get dimensions of UIView Class for CGRects?

I am currently working on an app that draws a series of rectangles ( CGRect ) on a view and sizes them based on the size of the UIView. 我目前正在开发一个在视图上绘制一系列矩形( CGRect )并根据UIView的大小CGRect设置大小的应用程序。 This code is contain in a class file that inherits UIView (rectangle.h/rectangle.m). 该代码包含在继承UIView(rectangle.h / rectangle.m)的类文件中。 I cannot for some reason use self.view.bounds.height or self.view.bounds.width to get the values I need. 由于某种原因,我无法使用self.view.bounds.heightself.view.bounds.width来获取所需的值。 I am using the overridden method " drawrect " that is built into the class. 我正在使用内置在类中的重写方法“ drawrect ”。 How can I get the view size? 如何获得视图尺寸? or if thats not possible, how can I implement this? 或者,如果那不可能,我该如何实现呢?

Use 采用

self.view.bounds.size.width 
self.view.bounds.size.height

or 要么

if self class is itself a UIView 如果self类本身是一个UIView

self.bounds.size.width 
self.bounds.size.height

You can get the view size using the following : 您可以使用以下方法获取视图大小:

CGSize viewSize = self.view.frame.size;

NSLog(@"View Size : %@",NSStringFromCGSize(self.view.frame.size));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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