2012-08-01 66 views
0

我必須創建一個始終居中居中對齊的標籤和按鈕,用戶將按任意順序選擇不同的項目,並且無論何時用戶選擇一個選項,它都應該顯示爲帶有標籤和十字按鈕的一行。當用戶點擊十字按鈕時,該選項將被取消選擇,剩餘的標籤和按鈕會將其位置設置在中心位置。如何在iOS中創建運行時動態標籤和按鈕?

你可能會得到一個好主意,用下面的圖像enter image description here

enter image description here enter image description here 看到「她的」有「X」按鈕。這裏所有的選擇的選項會

親切指導我

回答

2

因爲我只是一個冷靜的人,我決定花一些時間在這個問題上給你:)

從下載的例子我的收件箱:http://dl.dropbox.com/u/6487838/FSButtonLabelTest.zip

今天晚些時候我會在稍後解釋代碼。

編輯:添加了一些動畫。

+0

Awhh!我真的想要同樣的東西,這真棒。 thanx對你很好 – Mashhadi 2012-08-02 07:35:03

+0

在給定的鏈接上找不到文件 – 2017-12-16 13:09:35

0
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)]; 
    UIButton *btn =[[UIButton alloc]initWithFrame:CGRectMake(50,50,200,200)]; 
    [self.view addSubview:btn]; 
    [self.view addSubview:label]; 
1

添加拉布勒

UILabel *myLabel = [[UILabel alloc]init]; 
myLabel.frame  = CGRectMake(50,100,100,20); 
myLabel.text  = @"Your_Text"; 
[self.view addSubview:myLabel]; 

添加自定義按鈕,圖像

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
myButton.frame  = CGRectMake(210, 100, 20, 20); 
[myButton setImage:[UIImage imageNamed:@"cross.png"] forState:UIControlStateNormal]; 
[myButton addTarget:self action:@selector(removeFunction) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:myButton]; 

的按鈕替換removeFunction以您的功能爲目標。如果您不確定按鈕或座標的座標標籤,然後首先進入IB並將按鈕放置在您想要的位置,然後在右側檢查按鈕的座標以顯示尺寸檢查器,並在代碼中使用這些座標,這會讓您明白您的按鈕位置會去你添加它編程後...希望這會幫助你

+0

現在你怎麼能說這些動態中心對齊? – Mashhadi 2012-08-01 11:29:38

+0

好吧,他們是動態的,但不是確切的中心對齊,你必須根據你的視圖的設置來計算,因爲你知道worksapce尺寸是320X460 – superGokuN 2012-08-01 11:30:58