1

我已经动态地创建了这个按钮。它将使用我们数据库中的数据创建和填充,所以我不能使用 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];
4

3 回答 3

0

只需设置框架。

buttonWineries.frame = CGRectMake(...);
于 2013-05-23T02:30:44.753 回答
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)];
    }
}

我希望它对你有帮助。如果您仍然遇到任何问题,请告诉我。谢谢

于 2013-05-23T05:00:39.297 回答
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设为 aNSMutableArray而不是UIButton. 用于-addObject:将每个按钮添加到数组中。

于 2013-05-23T04:36:29.460 回答