在旋转时更改动态创建的按钮大小

时间:2013-05-23 00:15:49

标签: ios properties uibutton

我动态创建了这个按钮。它将使用我们数据库中的数据创建和填充,因此我无法使用IB。但是,在IB中,您可以声明@property并在以后执行操作(例如调整大小和旋转位置)。我的问题是;如何为动态创建的按钮赋予属性,以便在使用UIInterfaceOrientation旋转到横向时更改大小theOrientation = = self.interfaceOrientation;?

        UIButton *buttonWineries = [[UIButton alloc]initWithFrame:CGRectMake(10,yPositionWineries, 300, 160)];
        buttonWineries.layer.borderColor = [UIColor lightGrayColor].CGColor;
        buttonWineries.layer.borderWidth = 1.0;
        buttonWineries.layer.cornerRadius = 5;
        [buttonWineries setBackgroundColor:[UIColor darkGrayColor]];
        [self.wineriesMenu addSubview:buttonWineries];
        yPositionWineries += 190;
        [buttonWineries addTarget:self action:@selector(btnWineryDetails:) forControlEvents:UIControlEventTouchDown];

3 个答案:

答案 0 :(得分:0)

只需设置框架即可。

buttonWineries.frame = CGRectMake(...);

答案 1 :(得分:0)

您仍然可以使用@property使用您熟悉的相同语法为动态创建的视图声明属性(尽管您可以根据需要删除IBOutlet)。要将属性连接到按钮,而不是这样做:

UIButton *buttonWineries = [[UIButton alloc]initWithFrame:CGRectMake(10,yPositionWineries, 300, 160)];

这样做:

self.buttonWineries = [[UIButton alloc]initWithFrame:CGRectMake(10,yPositionWineries, 300, 160)];

使用self.buttonWineries访问按钮,就像使用Interface Builder插座一样。

如果您实际创建了多个不同的按钮,则可能需要将@property设为NSMutableArray而不是UIButton。使用-addObject:将每个按钮添加到数组中。

答案 2 :(得分:0)

我有更好的解决方案。只需在你的.h文件中执行一行即可实现此行

@interface yourViewController:UIViewController 
{
   UIButton *buttonWineries;
}

并将此代码实现到.m文件

-(void)ViewDidLoad
{

[super viewDidLoad];

   buttonWineries = [[UIButton alloc]init];
   buttonWineries.layer.borderColor = [UIColor lightGrayColor].CGColor;
   buttonWineries.layer.borderWidth = 1.0;
   buttonWineries.layer.cornerRadius = 5;
   [buttonWineries setBackgroundColor:[UIColor darkGrayColor]];
   [self.wineriesMenu addSubview:buttonWineries];
   yPositionWineries += 190;
   [buttonWineries addTarget:self action:@selector(btnWineryDetails:) forControlEvents:UIControlEventTouchDown];

   BOOL isPortrait = UIDeviceOrientationIsPortrait(self.interfaceOrientation);
   // now do whatever you need

   if(isPortrait)
     [buttonWineriessetFrame:CGRectMake(10,yPositionWineries, 300, 160)];
   else
     [buttonWineriessetFrame:CGRectMake(//your frame size for lanscape)];

}


// check your orientation angel and change your button size inside this delegate method

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
     // Return YES for supported orientations
     if (willRotateToInterfaceOrientation == UIInterfaceOrientationPortrait) {
         if (buttonWineries) 
            [buttonWineries setFrame:CGRectMake(//put your frame size here)];
     }
     if (willRotateToInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
          if (buttonWineries)   
            [buttonWineries setFrame:CGRectMake(//put your frame size here)];

     }
}

你可以添加didRotateFromInterfaceOrientation代替willRotateToInterfaceOrientation来在调整大小或改变位置时停止动画

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
     // Return YES for supported orientations
     if (willRotateToInterfaceOrientation == UIInterfaceOrientationPortrait) {
         if (buttonWineries) 
            [buttonWineries setFrame:CGRectMake(//put your frame size here)];
     }
     if (willRotateToInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
          if (buttonWineries)   
            [buttonWineries setFrame:CGRectMake(//put your frame size here)];

     }
    if (fromInterfaceOrientation == UIDeviceOrientationLandscapeRight) {
          if (buttonWineries)  
             [buttonWineries setFrame:CGRectMake(//put your frame size here)];
    }
}

我希望它可以帮到你。如果您仍然遇到任何问题,请告诉我。感谢