2011-04-25 31 views
0

我已創建了含有一個CGGradient視圖:CGGradient是不可見的(未使用界面生成器)和UIButtons不能觸發

// Bar ContextRef 
CGRect bar = CGRectMake(0, screenHeight-staffAlignment, screenWidth, barWidth); 
CGContextRef barContext = UIGraphicsGetCurrentContext(); 
CGContextSaveGState(barContext); 
CGContextClipToRect(barContext,bar);  

// Bar GradientRef 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
CGFloat components[16] = { 1.0,1.0,1.0,0.0, 0.0,0.0,0.0,1.0, 0.0,0.0,0.0,1.0, 1.0,1.0,1.0,0.0}; 
CGFloat locations[4] = {0.95,0.85,0.15,0.05}; 
size_t count = 4; 
CGGradientRef gradientRef = CGGradientCreateWithColorComponents(colorSpace, components, locations, count); 

// Draw Bar 
CGPoint startPoint = {0.0,0.0}; 
CGPoint endPoint = {screenWidth,0.0}; 
CGContextDrawLinearGradient(barContext, gradientRef, startPoint, endPoint, 0); 
CGContextRestoreGState(barContext);  

該代碼是所謂的的UIView drawRect方法。然後我使用UIViewController來訪問創建的視圖。

- (void)loadView { 

MainPageView *mpView = [[MainPageView alloc] initWithFrame:[window bounds]]; 
[self setView:mpView]; 
[mpView release]; 

}

並通過的appDelegate顯示在屏幕上:

mpViewController = [[MainPageViewController alloc] init]; 
[window addSubview:[mpViewController view]]; 

[window makeKeyAndVisible]; 

UIView的包含多個對象,諸如UIButtons,即是可見的。我假設,因爲他們被添加爲子視圖。但我無法解決如何將CGGradient添加爲子視圖?它需要成爲? CGGradient有沒有另外一個原因?

我也沒有得到UIButtons上的功能。我想這是因爲我已將UIButton添加到視圖中。是否需要在UIViewController或appDelegate中添加按鈕才能具有功能。不好意思問什麼看起來像是簡單的問題,但我試圖在沒有界面生成器的情況下完成編程,而且這些內容很少。如果任何人都可以指出我對這兩個問題的正確方向,我會非常感激。

謝謝!

回答

1

按鈕上的功能已丟失,因爲框架太大,但按鈕仍然可見,因爲背景是清晰的顏色