에 하위보기로 추가 할 UIImageView
에 대한 이미지의 정확한 위치를 설정하는 데 문제가 있습니다. 버튼을 만들고 몇 가지 하위 레이어를 추가하는 것으로 시작합니다.어떻게 UIImage 범위를 설정합니까?
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:bounds];
//add gradient background
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = button.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[UIColor colorWithRed:.5 green:.5 blue:.5 alpha:1.0].CGColor, (id)[[UIColor blackColor] CGColor], nil];
[button.layer insertSublayer:gradient atIndex:0];
//set green background
CALayer *layer = [CALayer layer];
layer.frame = CGRectInset(button.bounds, 5, 5);
layer.backgroundColor = [UIColor colorWithRed:.55 green:.8 blue:.5 alpha:1.0].CGColor;
[button.layer insertSublayer:layer above:gradient];
그런 다음 두 가지 방법으로 이미지를 설정합니다. 첫 번째 이미지 (이미지가 저장되지 않은 경우 사용)는 올바른 크기로 조정 된 이미지 리소스를 사용하며 앱 번들의 일부입니다. 다음 코드로 설정합니다 :
UIImageView *v = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"profile.png"]];
[v setBounds:CGRectInset(button.bounds, 8, 8)];
[button addSubview:v];
카메라를 통해 찍은 이미지를 사용하여 파일 시스템에 저장하면 문제가 발생합니다.
내가 잘못 뭐하는 거지 :
//path is an NSString set the the documents location of the image.
UIImageView *v = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:path]];
[v setBounds:CGRectInset(button.bounds, 8, 8)];
[v setContentMode:UIViewContentModeScaleToFill];
[button addSubview:v];
가 화면에 출력 지금 있어야 할 곳에 오프셋 이미지 방법을 보여줍니다 : 나는 다음과 같은 코드를 사용하여 배경이 설정하고 ? 상위 뷰에서 이미지의 크기를 올바르게 조정하려면 어떻게합니까? 간단히 버튼 이미지를 설정하려고했지만 아무것도 보이지 않았습니다 (아마도 이미지가 하위 레이어 뒤에 있었을 것입니다).
왜 그냥 설정 해달라고 이미지/배경 이미지가 될 코드를 대신
bounds
의 이미지 뷰의 프레임을 설정하는 방법? –@ LithuT.V, 나는 그것을 시도하고 아무 것도 보여주지 못했다. (왜냐하면 나는 버튼에'하위 계층 '을 추가하기 때문이다). – Phil
흥미 롭습니다. 그 코드도 추가하십시오. –