AutoresizingMask在Click事件中不起作用

时间:2013-05-16 05:35:28

标签: iphone ios objective-c

我在viewDidLoad()方法中创建了一个自定义按钮,并设置了Autoresizingmask。它运作良好。但是当我在Button Click事件中放入相同的代码时它不会自动化。还有其他方法可以访问Click事件中的AutoresizingMask吗?在此先感谢。

在此输入代码

- (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];
}

4 个答案:

答案 0 :(得分: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 :(得分: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){
                 }];

}

答案 2 :(得分:-1)

我认为您需要通过其点击事件强制转换您的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进行关联 看看这是否有效......

答案 3 :(得分:-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];
}
相关问题