iPhone UIButton setFrame 返回错误的框架大小

发布于 2024-10-18 12:43:24 字数 2146 浏览 1 评论 0原文

我正在尝试在 UIView 中创建一个特定大小的新按钮。碰巧的是,该视图将在 tableViewCell 中使用,如下所示。

UIImageCtrlBtnView *newView = [self initImageCtrlBtnsInViewWithHeight: 50 withWidth : 280];
[cell.contentView addSubview:newView];

我在 initImageCtrlBtnsInViewWithHeight 方法中创建了一个新视图和一堆按钮。

-(UIView*) initImageCtrlBtnsInViewWithHeight: (CGFloat) rowHeight withWidth: (CGFloat) rowWidth {

UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, rowWidth, rowHeight)];
newView.tag = kImageCtrlBtnViewTag;
newView.clipsToBounds = YES;

    // finding the right size of the button by setting the button size to some fraction
    // of the view size.
CGRect largeButtonFrame = CGRectMake(rowWidth*0.1+newView.frame.origin.x, rowHeight*0.2+newView.frame.origin.y, rowWidth*0.8, rowHeight*0.6);
UIButton *largeMoreButton = [UIButton buttonWithType:UIButtonTypeCustom];
largeMoreButton.tag = kLargeMoreBtnTag;
[largeMoreButton setTitle:@"Add Photo" forState:UIControlStateNormal ];         
[largeMoreButton setFrame: largeButtonFrame];

    // I check the size of the button frame.
   DLog(@"large more btn frame: %d, %d, %d, %d", largeMoreButton.frame.origin.x, largeMoreButton.frame.origin.y, largeMoreButton.frame.size.width, largeMoreButton.frame.size.height);    
etc.

问题是,当我在 setFrame 之后返回按钮时,它很大。这是日志的输出。按钮认为是1076101120高!

large more btn frame: 0, 1077706752, 0, 1076101120

我检查了调试日志中的 largeButtonFrame 矩形的值,它正如我所期望的那样。

(gdb) print largeButtonFrame
$1 = {
origin = {
   x = 28.5, 
   y = 10
}, 
size = {
  width = 228, 
  height = 30
}
}

最后,当视图显示在 tableView 中时,我收到此错误消息和一堆 CGContext 错误。

-[<CALayer: 0xff7b550> display]: Ignoring bogus layer size (320.000000, 1120403456.000000)
-[<CALayer: 0xff778a0> display]: Ignoring bogus layer size (300.000000, 1120403456.000000)

当我的表格显示时,按钮看起来大小合适,但该单元格基本上在表格视图中永远延伸,并且我无法滚动到其后面的任何其他单元格。

我还尝试以不同的方式设置按钮大小(例如设置按钮边界,然后将其居中于视图。或者使用 UIButton buttonWithType 创建器,然后设置框架),但这些似乎都没有什么区别。

谷歌搜索错误消息,此错误消息似乎仅在人们尝试制作动画时才会发生。但我并不想在这里做任何动画,只是设置一些按钮。

I am trying to create a new button of a specific size in a UIView. As it happens, this view will be used in a tableViewCell, like this.

UIImageCtrlBtnView *newView = [self initImageCtrlBtnsInViewWithHeight: 50 withWidth : 280];
[cell.contentView addSubview:newView];

I create a new view and a bunch of buttons in the method initImageCtrlBtnsInViewWithHeight.

-(UIView*) initImageCtrlBtnsInViewWithHeight: (CGFloat) rowHeight withWidth: (CGFloat) rowWidth {

UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, rowWidth, rowHeight)];
newView.tag = kImageCtrlBtnViewTag;
newView.clipsToBounds = YES;

    // finding the right size of the button by setting the button size to some fraction
    // of the view size.
CGRect largeButtonFrame = CGRectMake(rowWidth*0.1+newView.frame.origin.x, rowHeight*0.2+newView.frame.origin.y, rowWidth*0.8, rowHeight*0.6);
UIButton *largeMoreButton = [UIButton buttonWithType:UIButtonTypeCustom];
largeMoreButton.tag = kLargeMoreBtnTag;
[largeMoreButton setTitle:@"Add Photo" forState:UIControlStateNormal ];         
[largeMoreButton setFrame: largeButtonFrame];

    // I check the size of the button frame.
   DLog(@"large more btn frame: %d, %d, %d, %d", largeMoreButton.frame.origin.x, largeMoreButton.frame.origin.y, largeMoreButton.frame.size.width, largeMoreButton.frame.size.height);    
etc.

The problem is that when I get the button back after the setFrame, it is huge. Here is the output from the log. The button thinks it is 1076101120 high!

large more btn frame: 0, 1077706752, 0, 1076101120

I checked the value of the largeButtonFrame rectangle in the debug log, and it is as I would expect.

(gdb) print largeButtonFrame
$1 = {
origin = {
   x = 28.5, 
   y = 10
}, 
size = {
  width = 228, 
  height = 30
}
}

In the end, when the view is shown in the tableView, I get this error message, and a bunch of CGContext errrors.

-[<CALayer: 0xff7b550> display]: Ignoring bogus layer size (320.000000, 1120403456.000000)
-[<CALayer: 0xff778a0> display]: Ignoring bogus layer size (300.000000, 1120403456.000000)

When my table is shown, the buttons look like the right size, but this cell basically extends forever in the table view, and I can't scroll to any of the other cells behind it.

I've also tried to set the button size different ways (e.g. set the button bounds, and then center it to the view. Or use the UIButton buttonWithType creator, then setting the frame), but none of these seem to make a difference.

Googling the error message, this error message only seems to happen when folks are trying to do animation. But I am not trying to do any animation here, just setting some buttons.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

堇色安年 2024-10-25 12:43:24

呃,是的...这是一个漂浮物。

DLog(@"large more btn frame: %f, %f, %f, %f", largeMoreButton.frame.origin.x, largeMoreButton.frame.origin.y, largeMoreButton.frame.size.width, largeMoreButton.frame.size.height);    

根据您在运行应用程序时收到的警告,我不完全确定这些警告是什么,但我愿意打赌您没有使用浮点值作为大小,这只是基于数字它给了你,所以我不是 100% 确定。

Uh yea... it's a float.

DLog(@"large more btn frame: %f, %f, %f, %f", largeMoreButton.frame.origin.x, largeMoreButton.frame.origin.y, largeMoreButton.frame.size.width, largeMoreButton.frame.size.height);    

With the warnings you are getting when you're running the app, I'm not entirely sure what those are, but I'd be willing to bet you aren't using a float value for the size, that's just based on the numbers it's giving you, so I'm not 100% sure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文