我想在UIView中創建特定大小的新按鈕。碰巧,這個視圖將在tableViewCell中使用,就像這樣。iPhone UIButton setFrame返回錯誤的幀大小
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創建器,然後設置框架),但這些似乎都不是做出改變。
使用谷歌搜索錯誤消息,這個錯誤信息似乎只在人們試圖做動畫時發生。但我不想在這裏做任何動畫,只是設置一些按鈕。
就是這樣!我在我的heightForRow中返回了一個NSInteger,所以當創建正確的幀時,cell.contentView不是。 – Yenyi 2011-02-22 20:04:48