iPhone UIButton setFrame returns wrong frame size
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.or开发者_如何学编程igin.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.
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.
精彩评论