Bryce Langlotz
Bryce Langlotz

Reputation: 323

Drawing Rectangles in iOS

My goal for my app is for the user to be able to sort through different scheduling options by swiping to the left and right of an iPhone screen. How would I draw and remove rectangles as the user sorts through these different scheduling options?

I have a UIViewController.h, UIViewController.m, and UIViewController.xib files to manipulate. Do I need a separate UIView class? If so, how do I connect that UIView class to the view in my .xib file?

Upvotes: 11

Views: 41933

Answers (5)

Teja Nandamuri
Teja Nandamuri

Reputation: 11201

You can draw a rectangle by using this:

 UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake(68, 38, 81, 40)];
 [UIColor.grayColor setFill];
 [rectanglePath fill];

Upvotes: 6

Wilson Soethe Cursino
Wilson Soethe Cursino

Reputation: 261

Wow... so many complicated solution, here is what you need:

UIView *myBox  = [[UIView alloc] initWithFrame:CGRectMake(180, 35, 10, 10)];
myBox.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:myBox];

Nothing else... don't need to go crazy on the implementation.

Upvotes: 25

Jeremiah Smith
Jeremiah Smith

Reputation: 740

// 1st Add QuartzCore.framework to your project's "Link Binary with Libraries".

// 2nd Import the header in your .m file (of targeted view controller):

#import <QuartzCore/QuartzCore.h>

// 3rd Inside - (void)viewDidLoad method:
// Create a UIBezierPath (replace the coordinates with whatever you want):
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10.0, 10.0)];
// # Entry Point with 10px white frame
[path moveToPoint:CGPointMake(10.0, 10.0)];
// # Keeping 10px frame with iPhone's 450 on y-axis
[path addLineToPoint:CGPointMake(10.0, 450.0)];
// # Substracting 10px for frame on x-axis, and moving 450 in y-axis
[path addLineToPoint:CGPointMake(310.0, 450.0)];
// # Moving up to 1st step 10px line, 310px on the x-axis
[path addLineToPoint:CGPointMake(310.0, 10.0)];
// # Back to entry point
[path addLineToPoint:CGPointMake(10.0, 10.0)];

// 4th Create a CAShapeLayer that uses that UIBezierPath:
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = [path CGPath];
shapeLayer.strokeColor = [[UIColor blueColor] CGColor];
shapeLayer.lineWidth = 3.0;
shapeLayer.fillColor = [[UIColor clearColor] CGColor];
Add that CAShapeLayer to your view's layer:

// 5th add shapeLayer as sublayer inside layer view
[self.view.layer addSublayer:shapeLayer];

Upvotes: 12

Kekoa
Kekoa

Reputation: 28250

A UIView happens to draw in the shape of a rectangle, so if all you need is to change the color of the rectangle, set the backgroundColor property of the UIView to the color you want it to be.

An empty UIView will do:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
view.backgroundColor = [UIColor redColor];

If you are doing it from interface builder, drag a new View from the library onto the canvas and set the background color in the properties of the View.

You probably don't need a custom view for the simple case. You can also add additional items to your "rectangle" view such as UILabels for displaying text.

Upvotes: 3

bitmapdata.com
bitmapdata.com

Reputation: 9600

First Make a CustomView.

#import <UIKit/UIKit.h>

@interface MyView : UIView

@end

#import "MyView.h"

@implementation MyView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing Rect
    [[UIColor redColor] setFill];               // red
    UIRectFill(CGRectInset(self.bounds, 100, 100));
}


@end

you test. link to Your AppDelegate or ViewController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{    
    MyView* view = [[MyView alloc]initWithFrame:CGRectMake(10, 30, 300, 400)];
    view.backgroundColor = [UIColor blackColor];
    [window addSubview:view];

    [self.window makeKeyAndVisible];

    return YES;
}

- (void)viewDidLoad
{
    MyView* view = [[MyView alloc]initWithFrame:CGRectMake(10, 30, 300, 400)];
    view.backgroundColor = [UIColor blackColor];
    [self.view addSubview:view];

    [super viewDidLoad];
}

Upvotes: 5

Related Questions