我有一個ViewController
,它擁有六個按鈕。每個按鈕都有.tag
s 1到6,並且都連接到使用.tag
找出該做什麼的相同IBAction
。在4英寸設備上一切都很好。爲什麼我不能與我的按鈕進行交互?
然而,在3.5英寸的設備中,我需要重新安排按鈕以使其合適。我試圖只使用[self.view viewWithTag: tag]
來檢索按鈕,並更改檢索到的按鈕的.frame
,但這似乎沒有做任何事情。因此,我做了以下操作:
-(void) rearrangeButtons
{
int space = 7; // space between the first button and the left side of the screen as well as between individual buttons
for(int i = 0; i < 6; i++) {
// Get the button and remove from superview:
UIButton *button = (UIButton *)[self.view viewWithTag: i+1];
[button removeFromSuperview];
// Resize its width and height:
UIButton *newButton = [UIButton buttonWithType: UIButtonTypeCustom];
newButton.frame = CGRectMake(space + (i*(button.frame.size.width/2)) + (i*space), 170, button.frame.size.width/2, button.frame.size.height/2);
newButton.tag = button.tag;
//[newButton addTarget:self action:@selector(highlightButtonWithTag:) forControlEvents:UIControlEventTouchDown];
[newButton targetForAction:@selector(highlightButtonWithTag:) withSender:newButton];
[newButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"icon%i", i+1]] forState:UIControlStateNormal];
[self.view addSubview: newButton];
}
}
這將刪除按鈕並將它們放在新按鈕中。安排他們工作正常。然而,我想調用的選擇器沒有被調用,我似乎無法操作按鈕的屬性,因爲我可以使用之前做過的按鈕(即使這也完全基於它們的標籤)。
- (IBAction) selectIcon:(UIButton *)sender
{
NSLog(@"called");
// "Un"highlight previous button
UIButton *prevButton = (UIButton *)[self.view viewWithTag: self.selectedIcon];
prevButton.highlighted = NO;
// Highlight tapped button:
self.selectedIcon = sender.tag;
[self performSelector:@selector(highlightButtonWithTag:) withObject:@(self.selectedIcon) afterDelay:0];
}
我是什麼在這裏失蹤?謝謝!
你好弗洛裏安您正在craeting新鍵 「 //調整其寬度和高度: 的UIButton * newButton = [UIButton的buttonWithType:UIButtonTypeCustom];」對於改變幀你只需寫出button.frame = cgrectmake(放幀)。無需創建另一個按鈕 –
嘿 - 謝謝你的回覆!不過,我不太清楚你的代碼與我發佈的代碼有什麼不同。我使用'buttonWithType:'來創建一個'UIButton',然後像你所建議的那樣設置它的'.frame'屬性。或者我誤解了你? – Florian