Iphone 自动重新设置磁盘在单击事件中不工作

Iphone 自动重新设置磁盘在单击事件中不工作,iphone,ios,objective-c,Iphone,Ios,Objective C,我在viewDidLoad()方法中创建了一个自定义按钮,并设置了Autoresizingmask。它工作得很好。但当我在按钮点击事件中放入相同的代码时,它不会自动调整大小。是否有其他方法可以访问Click Events中的自动ResizingMask?提前谢谢 在此处输入代码 - (void)viewDidLoad { [super viewDidLoad]; UIButton *button = [UIButton buttonWithType:UIButtonTypeRounde

我在viewDidLoad()方法中创建了一个自定义按钮,并设置了Autoresizingmask。它工作得很好。但当我在按钮点击事件中放入相同的代码时,它不会自动调整大小。是否有其他方法可以访问Click Events中的
自动ResizingMask
?提前谢谢

在此处输入代码

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

我认为您需要通过点击事件来键入
ui按钮。将您的
触碰事件设置为这样

-(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];
}
并像这样调用此点击
@选择器(btn_clicked:)
或通过
界面生成器连接它

查看此项工作是否…

请使用以下代码可能对您有所帮助:

-(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];
}
  • (无效)viewDidLoad
{

forControlEvents:UIControlEventTouchDown]

[self.view addSubview:button];
}

-(iAction)btn_已单击:(id)发件人

{ UIButton btn=(UIButton)发送方

}

  • (无效)布局子视图
此方法的默认实现在iOS 5.1及更早版本上不起任何作用。否则,默认实现将使用您设置的任何约束来确定任何子视图的大小和位置

子类可以根据需要重写此方法,以便对其子视图执行更精确的布局。仅当子视图的自动调整大小和基于约束的行为不提供所需的行为时,才应重写此方法。可以使用实现直接设置子视图的框架矩形

您不应该直接调用此方法。如果要强制布局更新,请在下次图形更新之前调用setNeedsLayout方法。如果要立即更新视图的布局,请调用layoutIfNeeded方法

参考苹果公司的文件,如果自动重新定址任务不起作用,我们需要覆盖上述方法。在-(void)LayoutSubView中更改图幅时,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中都能正常工作。

您所说的“AutoresizingMask不工作”到底是什么意思?您对按钮单击的期望是什么?是自动重新设置任务不起作用还是添加子视图?我已经尝试过了。尽管它不起作用。请帮我解决这个问题。
[btn setTitle:@"Dynamic" forState:UIControlStateNormal];
[UIView animateWithDuration: 0.4
                 animations: ^{
                     btn.frame=CGRectMake(20, 73, 100, 43);
                     [UIView commitAnimations];
                 }completion: ^(BOOL finished){
                 }];
enter code here

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