0

次のコードを使用してナビゲーション バーのグラデーションを設定しようとしましたが、最後の行で EXC_BAD_ACCESS エラーでアプリがクラッシュします。

UIColor* topColor = [UIColor colorWithRed:245.0/255 green:245.0/255 blue:245.0/255 alpha:1.0];
UIColor* bottomColor = [UIColor colorWithRed:200.0/255 green:200.0/255 blue:200.0/255 alpha:1.0];
CAGradientLayer* navBarGradientLayer = [CAGradientLayer layer];
[navBarGradientLayer setFrame:CGRectMake(0, 0, self.navigationController.navigationBar.bounds.size.width, self.navigationController.navigationBar.bounds.size.height)];
NSArray* gradientColors = @[topColor, bottomColor];
navBarGradientLayer.colors = gradientColors;
[self.navigationController.navigationBar.layer insertSublayer:navBarGradientLayer atIndex:0];

ここで何が間違っていますか?

4

2 に答える 2

0
// get navigation bar objc
UINavigationBar *navigationBar= [[self.navigationController.navigationBar subviews]objectAtIndex:0];

//create uiview 
        UIView* gradientViewnavigationBar = [[UIView alloc] initWithFrame:CGRectMake(0,0,navigationBar.frame.size.width,navigationBar.frame.size.height)];

// create layer of gradient color 
CAGradientLayer * gradientLayernavigationBar = [[CAGradientLayer alloc] init];

gradientLayernavigationBar.frame = gradientViewnavigationBar.bounds;
gradientLayernavigationBar.colors = [NSArray arrayWithObjects:(id)[[UIColor redColor] CGColor], (id)[[UIColor  blackColor] CGColor], nil];

gradientLayernavigationBar.opacity=1.0;

// add layer on view 
// then add view on navigation bar   
[gradientViewnavigationBar.layer insertSublayer:gradientLayernavigationBar atIndex:0];
[navigationBar insertSubview:gradientViewnavigationBar atIndex:0];

それがあなたを助けることを願っています!

于 2015-06-17T07:13:46.833 に答える
0

これを試してください、それは私のために働いた

UINavigationBar *navigationBar= [[self.navigationController.navigationBar subviews]objectAtIndex:0];


UIView* gradientViewnavigationBar = [[UIView alloc] initWithFrame:CGRectMake(0,0,navigationBar.frame.size.width,navigationBar.frame.size.height)];


CAGradientLayer * gradientLayernavigationBar = [[[CAGradientLayer alloc] init] autorelease];

gradientLayernavigationBar.frame = gradientViewnavigationBar.bounds;
gradientLayernavigationBar.colors = [NSArray arrayWithObjects:(id)[startColornavigationBar CGColor], (id)[endColornavigationBar CGColor], nil];

gradientLayernavigationBar.opacity=1.0;

//グラデーションの角度を設定

  [CustomNavBar setAngleForGradients:gradientLayernavigationBar angle:[[nav_header_Element attributeForName:@"angle"]stringValue]];

[gradientViewnavigationBar.layer insertSublayer:gradientLayernavigationBar atIndex:0];
[navigationBar insertSubview:gradientViewnavigationBar atIndex:0];
[gradientViewnavigationBar release];
于 2012-12-15T15:20:39.023 に答える