2013-05-16 36 views
-2

我在viewDidLoad()方法中創建了一個自定義按鈕,並設置了Autoresizingmask。它運作良好。但是當我把相同的代碼放在Button Click事件中時,它不會自動調整。有沒有其他方法可以在點擊事件中訪問AutoresizingMask?提前致謝。AutoresizingMask無法在Click事件中工作

在這裏輸入代碼

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button setTitle:@"Show View" forState:UIControlStateNormal]; 

    button.backgroundColor=[UIColor redColor]; 
    button.frame=CGRectMake(20, 373, 73, 43); 
     button.autoresizingMask=(UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth); 
    [self.view addSubview:button]; 
} 


-(IBAction)btn_clicked 
{ 

UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [btn setTitle:@"Dynamic Btn" forState:UIControlStateNormal]; 

enter code here 
    btn.backgroundColor=[UIColor yellowColor]; 
    btn.frame=CGRectMake(20, 150, 73, 43); 
    btn.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; 
    [self.view addSubview:btn]; 
} 
+1

「AutoresizingMask Not Working」的含義是什麼?你期望按鈕點擊什麼? –

+0

它是AutoresizingMask不工作還是添加子視圖? –

回答

2
  • (空)layoutSubviews

此方法的默認實現不執行在iOS 5.1和更早版本罷了。否則,默認實現使用您設置的任何約束來確定任何子視圖的大小和位置。

子類可以根據需要重寫此方法以執行其子視圖的更精確佈局。只有在子視圖的自動調整大小和基於約束的行爲不提供所需行爲的情況下,才應該重寫此方法。您可以使用您的實現直接設置子視圖的框架矩形。

您不應該直接調用此方法。如果要強制佈局更新,請改爲在下一次更新圖紙之前調用setNeedsLayout方法。如果要立即更新視圖的佈局,請調用layoutIfNeeded方法。

如果AutoresizingMask無法正常工作,請參考Apple文檔,我們需要重寫上述方法。在 - (void)layoutSubviews中更改視圖框架時,AutoresizingMask可以工作。

enter code here 

-(void)layoutSubviews 
{ 
    NSLog(@"layoutSubviews called"); 
    CGRect viewsize=[[UIScreen mainScreen]bounds]; 
    view.frame=CGRectMake(0, 0, 320, viewsize.size.height); 
} 

嘗試以上代碼您的AutoresizingMask在iPhone4和iPhone5中都能很好地工作。

-1

我認爲你需要通過其click事件類型轉換UIButton。讓你的touchUpInside事件這樣

-(IBAction)btn_clicked:(id)sender 
{ 
    UIButton *btn = (UIButton*)sender; 
    [btn setTitle:@"Dynamic Btn" forState:UIControlStateNormal]; 

    //enter code here 
    btn.backgroundColor=[UIColor yellowColor]; 
    btn.frame=CGRectMake(20, 150, 73, 43); 
    btn.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; 
    [self.view addSubview:btn]; 
} 

並稱之爲點擊這樣@selector(btn_clicked:)或者通過Interface Builder 連接看看這項工作....

-1

請使用下面的代碼可以幫助你:

-(IBAction)btn_clicked 
{ 
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [btn setTitle:@"Dynamic Btn" forState:UIControlStateNormal]; 
    btn.backgroundColor=[UIColor yellowColor]; 
    btn.frame=CGRectMake(20, 150, 73, 43); 
    btn.autoresizesSubviews = YES; 
    self.view.autoresizesSubviews = YES; 
self.view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; 
btn.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; 
[self.view addSubview:btn]; 
} 
+0

我試過這個。請幫我解決這個問題。 – Saranya

0
  • (無效)viewDidLoad中

{

[super viewDidLoad]; 

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

[button setTitle:@"Show View" forState:UIControlStateNormal]; 

button.frame=CGRectMake(20, 73, 173, 43); 

[button addTarget:self action:@selector(btn_clicked:) 

forControlEvents:UIControlEventTouchDown];

[self.view addSubview:button]; 

}

- (IBAction爲)btn_clicked:(ID)發送方

{ 的UIButton BTN =(的UIButton)發送者;

[btn setTitle:@"Dynamic" forState:UIControlStateNormal]; 
[UIView animateWithDuration: 0.4 
       animations: ^{ 
        btn.frame=CGRectMake(20, 73, 100, 43); 
        [UIView commitAnimations]; 
       }completion: ^(BOOL finished){ 
       }]; 

}