-2

我在 viewDidLoad() 方法中创建了一个自定义按钮并设置了 Autoresizingmask。它运作良好。但是当我将相同的代码放在按钮单击事件中时,它不会自动调整大小。有没有其他方法可以访问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

4 回答 4

2
  • (void)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 中都运行良好。

于 2013-05-21T08:50:01.460 回答
0
  • (void)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 )sender;

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

}

于 2013-05-16T09:08:06.730 回答
-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 查看是否可以连接它....

于 2013-05-16T06:01:40.280 回答
-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];
}
于 2013-05-16T06:04:40.400 回答