2013-03-18 42 views
9

我認爲可能有一種方法可以使用自動佈局輕鬆地隱藏和顯示連續按鈕,以便視圖可以自動排列,這取決於哪些可見。使用自動佈局時優美地隱藏和顯示視圖

例如,說我有,我總是希望在一個框架爲中心的兩個按鈕:

// pseudo visual format code: 
|-----[star][download]-----| 

當我按下下載我現在希望看到三個按鈕:(pausedownload按鈕重新貼上標籤; cancel是先前隱藏的按鈕)

|--[star][cancel][pause ]--|

我想我也許可以有三個按鈕始終存在,但也許覆蓋寬度,使在狀態之間優雅地觀看動畫?我認爲可能有更多的語義方法來實現從自動佈局結構添加和刪除視圖。有什麼想法嗎?

+0

我問類似的問題,並得到了downvoted http://stackoverflow.com/問題/ 20876664/ios-autolayout-dynamic-adjust-controls/20876746?noredirect = 1#comment31327381_20876746 :) – Abhishek 2014-01-02 12:00:49

回答

0

我已經把顯示這怎麼會使用自定義UIView子類來完成一個小樣本。在下面的示例中,我使用了this answer中的AutoLayout framework,我建議您也這樣做;它使約束代碼保持清晰易讀。

一般的做法是,您必須保留指向左邊按鈕的後邊緣與右邊邊緣的關鍵約束的指針,然後使用這些指針動態地添加/移除約束。一般來說,你不想做太多的事情,因爲性能會受到影響,但響應用戶操作的一小部分是可以的。

我的看法是這樣宣稱:

@protocol TSDownloadViewDelegate; 
@interface TSDownloadView : UIView 
    @property (strong, nonatomic)   id<TSDownloadViewDelegate> delegate; 
@end 

@protocol TSDownloadViewDelegate <NSObject> 
    - (void) downloadStartedInDownloadView:(TSDownloadView*)downloadView; 
    - (void) downloadPausedInDownloadView:(TSDownloadView *)downloadView; 
    - (void) downloadCancelledInDownloadView:(TSDownloadView*)downloadView; 
@end 

而像這樣實現的:

#import "UIView+AutoLayout.h" 
#import "TSDownloadView.h" 

static const CGFloat  kMargin    = 20.0; 

@interface TSDownloadView() 

    // Our buttons 
    @property (strong, nonatomic)   UIButton * starButton; 
    @property (strong, nonatomic)   UIButton * cancelButton; 
    @property (strong, nonatomic)   UIButton * downloadButton; 

    // State tracking 
    @property (nonatomic)     BOOL   downloading; 
    @property (nonatomic)     BOOL   constraintsUpdated; 

    // The constraint governing what's tied to the right hand side of the starButton 
    @property (weak, nonatomic)   NSLayoutConstraint *starRightConstraint; 

    // The constraint governing what's tied to the left hand side of the downloadButton 
    @property (weak, nonatomic)   NSLayoutConstraint *downloadLeftConstraint; 

@end 

@implementation TSDownloadView 

- (void) initializator 
{ 
    _starButton = [UIButton buttonWithType:UIButtonTypeSystem]; 
    _cancelButton = [UIButton buttonWithType:UIButtonTypeSystem]; 
    _downloadButton = [UIButton buttonWithType:UIButtonTypeSystem]; 

    _starButton.translatesAutoresizingMaskIntoConstraints = NO; 
    _cancelButton.translatesAutoresizingMaskIntoConstraints = NO; 
    _downloadButton.translatesAutoresizingMaskIntoConstraints = NO; 

    _starButton.titleLabel.textAlignment = NSTextAlignmentCenter; 
    _cancelButton.titleLabel.textAlignment = NSTextAlignmentCenter; 
    _downloadButton.titleLabel.textAlignment = NSTextAlignmentCenter; 

    [_starButton setTitle:@"Star" forState:UIControlStateNormal]; 
    [_cancelButton setTitle:@"Cancel" forState:UIControlStateNormal]; 
    [_downloadButton setTitle:@"Download" forState:UIControlStateNormal]; 

    [_downloadButton addTarget:self action:@selector(downloadClicked:) forControlEvents:UIControlEventTouchUpInside]; 

    [self addSubview:_starButton]; 
    [self addSubview:_cancelButton]; 
    [self addSubview:_downloadButton]; 

    _cancelButton.hidden = YES; 

} 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     [self initializator]; 
    } 
    return self; 
} 

- (id) initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super initWithCoder:aDecoder]; 
    if(self) 
    { 
     [self initializator]; 
    } 
    return self; 
} 


- (void)downloadClicked:(id)sender 
{ 
    self.downloading = !self.downloading; 
    if(self.downloading) 
    { 
     [self.downloadButton setTitle:@"Pause" forState:UIControlStateNormal]; 
     self.cancelButton.hidden = NO; 

     // Remove previous constraints 
     [self removeConstraint:self.starRightConstraint]; 
     [self removeConstraint:self.downloadLeftConstraint]; 

     // |--[star][cancel][pause ]--| 
     self.starRightConstraint = [self.starButton autoPinEdge:ALEdgeRight toEdge:ALEdgeLeft ofView:self.cancelButton withOffset:-kMargin]; 
     self.downloadLeftConstraint = [self.downloadButton autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.cancelButton withOffset:kMargin]; 

     // Tell delegate what's happened 
     if(self.delegate) 
      [self.delegate downloadStartedInDownloadView:self]; 
    } 
    else 
    { 
     [self.downloadButton setTitle:@"Download" forState:UIControlStateNormal]; 
     self.cancelButton.hidden = YES; 

     // Remove previous constraints 
     [self removeConstraint:self.starRightConstraint]; 
     [self removeConstraint:self.downloadLeftConstraint]; 

     // |-----[star][download]-----| 
     self.starRightConstraint = [self.starButton autoPinEdge:ALEdgeRight toEdge:ALEdgeLeft ofView:self.downloadButton withOffset:-kMargin]; 
     self.downloadLeftConstraint = nil; 

     // Tell delegate what's happened 
     if(self.delegate) 
      [self.delegate downloadPausedInDownloadView:self]; 
    } 

} 

- (void) updateConstraints 
{ 
    [super updateConstraints]; 
    if(self.constraintsUpdated) return; 
     self.constraintsUpdated = YES; 

    // Now put our constraints in place 

    // Make sure the button hugs the label and doesn't get stretched 
    // just because there's space available 
    [self.starButton setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]; 

    // Pin the starButton to the top, left and bottom edges of its superview 
    [self.starButton autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:kMargin]; 
    [self.starButton autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:kMargin]; 
    [self.starButton autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:kMargin]; 

    // Repeat for the other buttons 

    [self.cancelButton setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]; 
    [self.cancelButton autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:kMargin]; 
    [self.cancelButton autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:kMargin]; 

    [self.downloadButton setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]; 
    [self.downloadButton autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:kMargin]; 
    [self.downloadButton autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:kMargin]; 
    [self.downloadButton autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:kMargin]; 


    // These two are special. We keep a reference to them so we can replace 
    // them later. Note that since the cancelButton is hidden at the start, 
    // the initial value for downloadLeftConstraint is simply nil. 

    self.starRightConstraint = [self.starButton autoPinEdge:ALEdgeRight toEdge:ALEdgeLeft ofView:self.downloadButton withOffset:-kMargin]; 
    self.downloadLeftConstraint = nil; 
} 
@end 

還有很多工作要做,以使視圖真正發揮作用,但希望你能看到一般採取的方法。

0

使用Autolay來設計(5)按鈕。

//在viewDidLoad中:設置取消&暫停按鈕隱藏

-(void) viewDidLoad 
{ 
    [_pauseBtn setHidden:YES]; 
    [_cancelBtn setHidden:YES]; 
} 

//就立即下載行動

-(IBAction) downloadClick (UIButton *) sender 
{ 
    [_pauseBtn setHidden:NO]; 
    [_cancelBtn setHidden:NO]; 
    [sender setHidden:YES]; 
} 
相關問題