繁体   English   中英

在Objective-C中为iO绘制矩形

[英]Draw rectangle(s) in Objective-C for iOs

我是Objective C的初学者,我想在iPhone屏幕上绘制一个矩形。

我所做的是:

-创建一个名为CustomView的新类

-修改AppDelegate.m文件,如下所示:

#import "AppDelegate.h"
#import "CustomView.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
CGRect bigRect =  CGRectMake (100, 100, 100, 100) ;
CGRect smallRect =  CGRectMake (50, 50, 50, 50);
CustomView * bigView = [[ CustomView alloc ]  initWithFrame:bigRect ];
CustomView * smallView = [[ CustomView alloc ]  initWithFrame:smallRect ];
[ bigView setBackgroundColor :[ UIColor redColor ]];
[ smallView setBackgroundColor :[ UIColor blueColor ]];
[ self.window addSubview:bigView ];
[ self.window addSubview:smallView ];

return YES;

}

当我模拟这一点时,我只会看到白色的屏幕。 我怀疑我可能需要更新CustomView类本身吗?

为什么要使用appDelegate? 您需要添加方法以在ViewController实现中创建用户界面。

请尝试使用更简单的解决方案:

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

然后在viewDidLoad中调用该方法。 CGRectMake参数是x,y,高度和宽度,您可能需要根据需要对其进行编辑。

我也建议您查看这篇文章-https://www.google.ru/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CBsQFjAAahUKUKEwj0 - vXRz4PJAhWCjiwKHd36Bo0&url=https%3A%2F%2Fdeveloper.apple.com% 2Flibrary%2Fios%2Fdocumentation%2FUIKit%2FReference%2FUIView_Class%2F&USG = AFQjCNHj3LrxW69BtP-2db5kevpbQ_AuKw&SIG2 = LTnzngT5im-5Jr8wWNeqbQ及CAD = RJT

暂无
暂无

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

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