2013-07-02 45 views
2

我想在CGRect中添加邊框。雖然以前的解決方案建議要麼這樣:在CGRect中添加邊框iOS

CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0); 
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0); 
    CGContextFillRect(context, rectangle); 

或者是這樣的:

view.layer.borderWidth = 10; 
view.layer.borderColor = [UIColor redColor].CGColor; 

我不能在我的方法或者適用這一點。

在我MAINVIEW控制器我有這樣的代碼:

-(void)actionHint 
{ 
    CGRect viewRect = CGRectMake(kScreenWidth/2 -kMenuWidth, kScreenHeight/2-kMenuHeight - 70,kMenuWidth*10, kMenuHeight*4); 
    HintMenu* hintMenu = [HintMenu viewWithRect:viewRect]; 
    [hintMenu.btnReveal addTarget:self action:@selector(actionReveal) forControlEvents:UIControlEventTouchUpInside]; 
    [hintMenu.btnNewAnag addTarget:self action:@selector(actionNewAnagram) forControlEvents:UIControlEventTouchUpInside]; 
    [self.gameView addSubview:hintMenu]; 
} 

我創建一個比賽,我想用一些幫助操作的簡單矩形來對我的當前視圖的頂部。 viewRect的代碼是:

+(instancetype)viewWithRect:(CGRect)r 
{ 
    HintMenu* hint = [[HintMenu alloc] initWithFrame:r]; 
    hint.userInteractionEnabled = YES; 

    UIImage* image=[UIImage imageNamed:@"btn"]; 
    ... rest of items in the cgrect 
    return hint; 
} 

我應該在哪裏添加用於添加邊框的代碼以及代碼應該是什麼?

回答

2

HintMenu是一種類型的視圖,以便在其initWithFrame方法,你可以這樣做:

self.layer.borderWidth = 10; 
self.layer.borderColor = [UIColor redColor].CGColor; 
0

您可以使用此代碼創建一個邊矩形。你所要做的就是導入QuartzCore框架。隨意複製+粘貼此代碼。

#import <QuartzCore/QuartzCore.h> 

-(void)viewDidLoad{ 
    //give it a blue background 
    self.layer.backgroundColor=[UIColor blueColor].CGColor; 
    //set the boarder width 
    self.layer.borderWidth=5; 
    //set the boarder color 
    self.layer.borderColor=[UIColor redColor].CGColor;  
}