我已经动态创建了这个按钮。它将被创建并填充来自我们数据库的数据,所以我不能使用IB。然而,在IB中,你可以声明一个@属性,并在以后做一些事情(比如旋转时调整大小和位置)。我的问题是:如何为动态创建的按钮提供属性,以便我可以在使用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];发布于 2013-05-23 13:00:39
我有一个更好的解决方案。只需做一件事,即在.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-23 10:30:45
只需设置框架即可。
buttonWineries.frame = CGRectMake(...);发布于 2013-05-23 12:36:29
您仍然可以使用@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:将每个按钮添加到数组中。
https://stackoverflow.com/questions/16703644
复制相似问题