首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在旋转时更改动态创建的按钮大小

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

Stack Overflow用户
提问于 2013-05-23 08:15:49
回答 3查看 640关注 0票数 1

我已经动态创建了这个按钮。它将被创建并填充来自我们数据库的数据,所以我不能使用IB。然而,在IB中,你可以声明一个@属性,并在以后做一些事情(比如旋转时调整大小和位置)。我的问题是:如何为动态创建的按钮提供属性,以便我可以在使用UIInterfaceOrientation theOrientation == self.interfaceOrientation;旋转到横向时更改大小?

代码语言:javascript
运行
复制
        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];
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-05-23 13:00:39

我有一个更好的解决方案。只需做一件事,即在.h文件中实现这一行

代码语言:javascript
运行
复制
@interface yourViewController:UIViewController 
{
   UIButton *buttonWineries;
}

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

代码语言:javascript
运行
复制
-(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来在调整大小或更改位置时停止动画

代码语言:javascript
运行
复制
- (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)];
    }
}

我希望它能对你有所帮助。如果您仍然面临任何问题,请让我知道。谢谢

票数 0
EN

Stack Overflow用户

发布于 2013-05-23 10:30:45

只需设置框架即可。

代码语言:javascript
运行
复制
buttonWineries.frame = CGRectMake(...);
票数 0
EN

Stack Overflow用户

发布于 2013-05-23 12:36:29

您仍然可以使用@property来声明动态创建的视图的属性,使用您所熟悉的语法(不过如果您愿意,也可以删除IBOutlet )。要将属性连接到按钮,而不是执行以下操作:

代码语言:javascript
运行
复制
UIButton *buttonWineries = [[UIButton alloc]initWithFrame:CGRectMake(10,yPositionWineries, 300, 160)];

只需执行以下操作:

代码语言:javascript
运行
复制
self.buttonWineries = [[UIButton alloc]initWithFrame:CGRectMake(10,yPositionWineries, 300, 160)];

使用self.buttonWineries访问该按钮,方法与使用Interface Builder插座时的方式相同。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16703644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档