int i; int n = 0; //新建一個視圖類對象並設置它的大小 UIScrollView *newview =[[UIScrollView alloc]init]; //依次為它的x y位置,長和寬 newview.frame =CGRectMake(0, 0, 300, 400); //把這個對象加到view中去。顯示出來 [self.view addSubview:newview]; //添加10個Button for(i =0; i<10;i++) { //新建一個button對象 button還有一些別的屬性比如背景色 //buttonview.backgroundColor = [UIColor clearColor]; UIButton *buttonview = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //為button顯示賦值 [buttonview setTitle:@"testbutton" forState:UIControlStateNormal]; //設置button的大小 buttonview.frame = CGRectMake(20, 20+n, 280, 20); [newview addSubview:buttonview]; //[self.view addSubview:button2]; n = n+30; NSLog(@"%i",n); }
//可實現滑動 [newview setContentSize:CGSizeMake(320, 420)];
[newview release];
下面這段轉自:http://my.oschina.net/clownfish/blog/56812
//這里創建一個圓角矩形的按鈕 UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // 能夠定義的button類型有以下6種, // typedef enum { // UIButtonTypeCustom = 0, 自定義風格 // UIButtonTypeRoundedRect, 圓角矩形 // UIButtonTypeDetailDisclosure, 藍色小箭頭按鈕,主要做詳細說明用 // UIButtonTypeInfoLight, 亮色感嘆號 // UIButtonTypeInfoDark, 暗色感嘆號 // UIButtonTypeContactAdd, 十字加號按鈕 // } UIButtonType; //給定button在view上的位置 button1.frame = CGRectMake(20, 20, 280, 20); //button背景色 button1.backgroundColor = [UIColor clearColor]; //設置button填充圖片 //[button1 setImage:[UIImage imageNamed:@"btng.png"] forState:UIControlStateNormal]; //設置button標題 [button1 setTitle:@"點擊" forState:UIControlStateNormal]; /* forState: 這個參數的作用是定義按鈕的文字或圖片在何種狀態下才會顯現*/ //以下是幾種狀態 // enum // UIControlStateNormal = 0, 常規狀態顯現 // UIControlStateHighlighted = 1 << 0, 高亮狀態顯現 // UIControlStateDisabled = 1 << 1, 禁用的狀態才會顯現 // UIControlStateSelected = 1 << 2, 選中狀態 // UIControlStateApplication = 0x00FF0000, 當應用程序標志時 // UIControlStateReserved = 0xFF000000 為內部框架預留,可以不管他 // }; /* * 默認情況下,當按鈕高亮的情況下,圖像的顏色會被畫深一點,如果這下面的這個屬性設置為no, * 那么可以去掉這個功能 */ button1.adjustsImageWhenHighlighted = NO; /*跟上面的情況一樣,默認情況下,當按鈕禁用的時候,圖像會被畫得深一點,設置NO可以取消設置*/ button1.adjustsImageWhenDisabled = NO; /* 下面的這個屬性設置為yes的狀態下,按鈕按下會發光*/ button1.showsTouchWhenHighlighted = YES; /* 給button添加事件,事件有很多種,我會單獨開一篇博文介紹它們,下面這個時間的意思是 按下按鈕,並且手指離開屏幕的時候觸發這個事件,跟web中的click事件一樣。 觸發了這個事件以后,執行butClick:這個方法,addTarget:self 的意思是說,這個方法在本類中 也可以傳入其他類的指針*/ [button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside]; //顯示控件 [self.view addSubview:button1];
//在UIScrollView中添加的button與事件關聯。。 //UIButtonTypeCustom 這個屬性表示不顯示按鈕 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [newscrollview addSubview:button]; button.frame = CGRectMake(0, 0, 300, 200); //button.backgroundColor = [UIColor redColor]; button.tag = 0; //UIButton *buttonview = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //[buttonview setTitle:[userinfo objectAtIndex:i] forState:UIControlStateNormal]; //關聯事件。。butttest [button addTarget:self action:@selector(butttest:) forControlEvents:UIControlEventTouchUpInside]; //通過button.tag 就能與多個button寫不同的事件。。 -(IBAction)butttest:(id)sender { NSLog(@"all all all ...\n"); if( 0 == ((UIButton*)sender ).tag) { NSLog(@"test ok..\n"); } }