2013-07-23 19 views
0

我不知道如何搜索這個問題的答案,所以我想我會在這裏嘗試我的運氣。簡而言之,我有一個UIView並創建了一個UIButton以及一個UIImageView作爲子視圖添加到按鈕中,當按下按鈕時,它允許我添加一個圖像並它被存儲在按鈕上。我怎樣才能創建一個UIButtons /(UIImageViews)出現在最後一個被按下後的數組

所以我的問題是:一旦完成,我怎麼能讓另一個按鈕/ ImageView將出現在視圖中,第一個按鈕旁邊(或在它填充右邊的屏幕後,出現在第二個行,第一列)等

謝謝你先進!

回答

0

您可以嘗試使用UICollectionView解決此問題。

0
@interface ViewController(){ 

    NSInteger x; 
    NSInteger y; 
} 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    x = 4;y=100; 


    // Do any additional setup after loading the view, typically from a nib. 
} 


- (IBAction)addViews:(id)sender { 

    CGRect frame = CGRectMake(x, y, 40, 10); 

    int offset = 30; 

    x = (frame.origin.x + frame.size.width + offset); 

    UIView *view = [[UIView alloc] initWithFrame:frame]; 
    [view setBackgroundColor:[UIColor redColor]]; 

    if (view.center.x+view.frame.size.width > 320) { 
     y +=20; 
     x = 4; 
    } 
    [view setFrame:frame]; 

    [self.view addSubview:view]; 



} 

@end 
+0

謝謝,這是工作對於我想要做的事情...只要確保它拿起我需要的一切 – D34thSt4lker

1

您可以使用@Bernahard Harrer指出的UICollectionView或者您可以使用下面的代碼。此代碼只添加兩個按鈕。在情況下,需要更多的按鈕或按鈕的陣列,實現下面的代碼,並計算先前添加的按鈕的大小,作爲button1高度/寬度被計算用於添加button2

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
    [self addButtons]; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


-(void) addButtons 
{ 
    UIView *view = [self view]; 
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button1 setBackgroundImage:[UIImage imageNamed:@"buttonImage1"] forState:UIControlStateNormal]; 
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button2 setBackgroundImage:[UIImage imageNamed:@"buttonImage2"] forState:UIControlStateNormal]; 

[button1 sizeToFit]; 
[button2 sizeToFit]; 


CGFloat combinedWidth = (button1.frame.size.width) + (button2.frame.size.width) ; 

//checking if combined width of both buttons is more than 320 
if (combinedWidth>320) { 
    CGFloat button1height = button1.frame.size.height; 
    button1height = button1height + 10; 
    [button2 setFrame:CGRectMake(button2.frame.origin.x, (button2.frame.origin.y + button1height), button2.frame.size.width, button2.frame.size.width)]; 
} else { 
    //if combined width of both buttons is less than or equal to 320 
    //this will place the buttons adjacent to each other. 
    //The code may be modified accordingly if to add some distance between them 
    CGFloat button1width = button1.frame.size.width; 
    [button2 setFrame:CGRectMake((button1.frame.origin.x+button1width), button2.frame.origin.y, button2.frame.size.width, button2.frame.size.height)]; 
} 

[view addSubview:button1]; 
[view addSubview:button2]; 
}@end 
+1

@ D34thSt4lker:我已經將圖像添加到按鈕作爲背景,而不是子視圖。無論哪種情況,此代碼都應該可以工作。 – iphondroid

相關問題