ios - Draw Rectangle in View -


I want to draw a rectangle in my ViewController2.

I have read that I can use CGRCAC but I want to represent a room, so I can put a dot in that position where I am in the room .

I tried to override the direct method already but I had some errors

I tried with this code:

  // rectangle.h #import & lt; UIKit / UIKit.h & gt; @ Interface rectangle: UIView @property (nonatomic, assign) CRGect myRect; @end // rectangle.m #import "rectangle.h" @create the implementation rectangle @ myRect; - (Zero) Direct: (NSR) Rect {// Drawing Code is here. CGContextRef c = UIGraphicsGetCurrentContext (); CGContextSetRGBFillColor (c, 1.0, 0.0, 0.5, 1.0); CGContextFillRect (c, myRect); } @ And // View Controller 2.M # Imports "View Controller 2.h" #import "rectangle.h" @ Implementation View Controller 2 // Apply viewDidloadload to make additional setup after loading the view. - (zero) viewDidoadload [[Super Viewedload]; If (! [Self-description is Kindofclass: rectangle]) {rectangle * visible = [[rectangle withframe: self.view.bounds]; Self.view = View; } [(Rectangle *) self.view setmart: CGRactMake (120, 120, 80, 80)]; [Self.view settingsdisplay]; } @end  

but it does not work

Do not you think that the CGRact is a stark and not an object? It defines the origin and size of a rectangle point that you need to use with UIView or other UI class. As a user 2277872, a reiki indicates that it takes a CGRact as its frame - it's assigning the point of origin (XI) and its width in the scene and Height (W, D)

To get it in your visual controller view, you have to add it as a subview.

CGFFrame = CGRactam (0, 0, 200, 400);

Then UIView * v = [[UIView alloc] initWithFrame: frame];

Again @ user2277872 has indicated that you can do it in the viewdidload method

It just seems that he has not told you that you should write < / P>

  [self.view addSubview: v];  

The second issue to note is that the view is usually white in general, so you have to set the background color if you want to see it

  [V setBackgroundColor: [UIColor redColor]];  

You should see a rectangle at the top of the view.

As you are using the storyboard, you can only drag a view to the view controller and set its color and size there.


Comments