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

Ios 在旋转时更改动态创建的按钮大小,ios,properties,uibutton,Ios,Properties,Uibutton,我已经动态创建了这个按钮。它将由我们数据库中的数据创建和填充,因此我不能使用IB。但是,在IB中,您可以声明@property,并在以后执行一些操作(如调整大小和旋转时的位置)。我的问题是,;如何为动态创建的按钮赋予属性,以便在使用UIInterfaceOrientation theOrientation==self.interfaceOrientation;旋转到横向时更改大小 UIButton *buttonWineries = [[UIButton alloc]initWi

我已经动态创建了这个按钮。它将由我们数据库中的数据创建和填充,因此我不能使用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];
只需设置框架

buttonWineries.frame = CGRectMake(...);

您仍然可以使用熟悉的语法使用
@property
为动态创建的视图声明属性(如果需要,您可以删除
IBOutlet
)。要将属性连接到按钮,请执行以下操作:

UIButton *buttonWineries = [[UIButton alloc]initWithFrame:CGRectMake(10,yPositionWineries, 300, 160)];
只要这样做:

self.buttonWineries = [[UIButton alloc]initWithFrame:CGRectMake(10,yPositionWineries, 300, 160)];
使用
self.buttonWineries
访问按钮,方法与界面生成器插座相同


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

我有一个更好的解决方案。只需做一件事,在.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来代替WillRotateInterfaceOrientation,以在调整大小或位置更改时停止动画

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

我希望它能帮助你。如果你还面临任何问题,请告诉我。谢谢

看看这个问题的答案。谢谢,但有三个答案。不确定要查看哪一个。使用带标记的superview。文档的链接也是一个非常有用的来源。我会设法弄明白的。因为我正在从数据库返回大量数据,我需要对其进行样式设置。这一切在纵向效果都很好,我可以在循环中设置样式和放置东西,但由于样式设置发生在与旋转不同的位置,因此我只能在旋转中重新定位东西和一些样式元素。动态创建的标签和文本视图也会出现这种情况。我确实做到了,并且得到了一条可靠的老消息“使用未声明的标识符”,如果使用WillAnimateRotationInterfaceOrientation:duration而不是willRotateToInterfaceOrientation:,不是更好吗?通过这种方式调整按钮的大小将使其具有动画效果,从而改善用户体验。但是我需要把按钮放在我的for循环中。将其放入viewDidLoad只创建了一个按钮。当我旋转它时,它确实会改变。但当我把它放回for循环时,什么也没发生。也许我应该提到它在for循环中:-(void)putwinersbuttonsinscollview:(int)numberOfButtons{for(int i=0;i这取决于你想要的情况。我需要动画,所以我用WillAnimateRotationInterfaceOrientation:duration实现了它。如果任何人不需要任何动画,他/她可以使用didRotateFromInterfaceOrientation:method for Iti user1510450,好的,你只需检查一下你的按钮是否存在于你的viewcontr中oller然后将其设置为任何帧大小。请查看我的更新代码。当我旋转时,这正在工作。问题:如果我将按钮代码放在viewDidLoad或for循环中,它希望首先加载纵向视图,因为我们在纵向视图中创建了按钮。因此,如果我在横向视图中输入页面,它将加载纵向视图,然后我就有了若要旋转,它将开始接收来自旋转方法的消息。