2014-10-07 1 views
2

둥근 모서리가 아닌 사각형을 사용하려고하는 UIProgressView가 있습니다. 이렇게하려면 분명한 방법이 없기 때문에 진행 이미지 자체를 사각 프레임보다 약간 크게하여 진행 이미지가 사각형 모양으로 잘 리도록하는 것이 좋습니다. 그러나 진행률 이미지의 크기를 어떻게 변경하든이 효과를 얻을 수는 없습니다. 내가하려는 일을 성취 할 수있는 방법이 있는지 누군가가 말해 줄 수 있습니까? 여기 둥근 모서리 효과보다 정사각형 만들기 UIProgressView

내가 현재 UIView 하위 내부에있는 것입니다 :

self.progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; 
self.progressView.trackImage = [PLStyle imageForColor:[PLColors greyLight] inFrame:CGRectMake(0, 0, 1, ProgressViewHeight)]; 
self.progressView.progressImage = [PLStyle imageForColor:[PLColors orange] inFrame:CGRectMake(0, 0, 1, ProgressViewHeight)]; 

[self.progressView setTranslatesAutoresizingMaskIntoConstraints:NO]; 
[self addSubview:self.progressView]; 

[self addConstraint: 
[NSLayoutConstraint constraintWithItem:self.progressView 
           attribute:NSLayoutAttributeLeading 
           relatedBy:NSLayoutRelationEqual 
           toItem:self.imageView 
           attribute:NSLayoutAttributeTrailing 
          multiplier:1 
           constant:LeadingOrTrailingSpace]]; 

[self addConstraint: 
[NSLayoutConstraint constraintWithItem:self.progressView 
           attribute:NSLayoutAttributeCenterY 
           relatedBy:NSLayoutRelationEqual 
           toItem:self 
           attribute:NSLayoutAttributeCenterY 
          multiplier:1 
           constant:0]]; 

[self addConstraint: 
[NSLayoutConstraint constraintWithItem:self.progressView 
           attribute:NSLayoutAttributeTrailing 
           relatedBy:NSLayoutRelationEqual 
           toItem:self.counter 
           attribute:NSLayoutAttributeLeading 
          multiplier:1 
           constant:-LeadingOrTrailingSpace]]; 

[self addConstraint: 
[NSLayoutConstraint constraintWithItem:self.progressView 
           attribute:NSLayoutAttributeHeight 
           relatedBy:NSLayoutRelationEqual 
           toItem:nil 
           attribute:NSLayoutAttributeNotAnAttribute 
          multiplier:1 
           constant:ProgressViewHeight]]; 

// 당신은 항상 자신의 롤 수

+ (UIImage *)imageForColor:(UIColor *)color inFrame:(CGRect)rect 
{ 
    UIGraphicsBeginImageContext(rect.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetFillColorWithColor(context, [color CGColor]); 
    CGContextFillRect(context, rect); 

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return image; 
} 

답변

2

진행 뷰에서 이미지를 만들기 위해 위에서 사용 된 방법 정의 꽤 쉽게.

@interface MyProgressView : UIView 
@property (assign) float progress; 
@end 

@implementation MyProgressView { 
    UIView *progressBar; 
} 
- (void)setProgress:(float)progress { 
    _progress = progress; 
    [self setNeedsLayout]; 
} 
- (void)layoutSubviews { 
    CGRect frame = self.bounds; 
    frame.size.width *= progress; 
    progressBar.frame = frame; 
} 
@end 

"최소/최대 값"+ "설정 값"을 사용하고 응용 프로그램에 더 적합한 경우 진행 계산을 수행 할 수도 있습니다. 간단한 직사각형을 그리기 위해 UIView의 무리를 함께 뭉개 버리는 것을 싫어하면 CALayer을 사용할 수도 있습니다.

0

사용 progressViewStyle = .bar 인

let p = UIProgressView(frame: .zero) 
p.progressViewStyle = .bar 
1

설정 UIProgressViewStyle

bar에 목적-C :

[progressView setProgressViewStyle: UIProgressViewStyleBar]; 

스위프트 :

progressView.progressViewStyle = .bar