我想與滾動視圖一起動態地添加按鈕,假設有100個按鈕要添加到滾動視圖上,將它添加到nib文件上會是一件容易的事,所以我想知道如何編寫代碼,在滾動視圖的圖像視圖頂部動態添加按鈕如何在滾動視圖上添加多個按鈕
4
A
回答
7
你需要做的是創建一個循環,創建UIButtons
。設置按鈕&將它們作爲子視圖添加到UIScrollView
。代碼如下。
NSUInteger i;
int xCoord=0;
int yCoord=0;
int buttonWidth=100;
int buttonHeight=50;
int buffer = 10;
for (i = 1; i <= 100; i++)
{
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight);
[aButton addTarget:self action:@selector(whatever:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:aButton];
yCoord += buttonHeight + buffer;
}
[scrollView setContentSize:CGSizeMake(700, yCoord)];
我基本上這裏做的是具有X & Y座標的變量是什麼。當我循環創建UIButtons
上午創建適當的CGRect
結構來決定將按鈕放在UIScrollView
的哪裏。將該按鈕添加到scrollView後,將Y值更改爲您想要放置下一個按鈕的位置。
最後不要忘記爲scrollview設置ContentSize
,以便它能夠滾動。 PS:所有這些代碼都是自由輸入的,可能有小的語法錯誤,但是邏輯是可靠的。
0
可以使用以下代碼來對滾動視圖添加多個按鈕:
步驟1:
Delegate "UIScrollViewDelegate" to your Viewcontroller.h
for example:
@interface Viewcontroller : UIViewController<UIScrollViewDelegate>
步驟2:
//create you UIScrollView
UIScrollView *Scroll_View= [[UIScrollView alloc]initWithFrame:CGRectMake(0 ,0 ,self.view.frame.size.width ,self.view.frame.size.height-0)];
Scroll_View.delegate= self;
self.automaticallyAdjustsScrollViewInsets= NO;
Scroll_View.backgroundColor= [UIColor clearColor];
Scroll_View.scrollEnabled= YES;
Scroll_View.userInteractionEnabled= YES;
[Scroll_View setShowsHorizontalScrollIndicator:NO];
[Scroll_View setShowsVerticalScrollIndicator:YES];
[self.view addSubview:Scroll_View];
步驟3:
NSArray *optionsAry= [NSArray arrayWithObjects:@"My Profile & My Orders" ,@"Track Order" ,@"Settings" ,@"Contact Us" ,@"About Us" ,@"Terms & Conditions" ,@"Currency Converter" ,@"System Info" ,@"e-Commerce News App (News Billa)" ,@"Social Media" ,nil];
int y= 20;
for(int i=0; i<optionsAry.count; i++){
UIButton *BTN_For_More= [[UIButton alloc] initWithFrame:CGRectMake(20 ,y , Scroll_View.frame.size.width-40 ,35)];
BTN_For_More.tag= i;
BTN_For_More.backgroundColor= [UIColor whiteColor];
[BTN_For_More addTarget:self action:@selector(BTN_For_More_Action:) forControlEvents:UIControlEventTouchUpInside];
BTN_For_More.titleLabel.text= [NSString stringWithFormat:@"%@",[optionsAry objectAtIndex:i]];
BTN_For_More.titleLabel.textColor= [UIColor colorWithRed:12/255.0 green:104/255.0 blue:172/255.0 alpha:1.0f];
BTN_For_More.titleLabel.textAlignment= NSTextAlignmentCenter;
BTN_For_More.titleLabel.font= [UIFont fontWithName:@"SourceSansPro-Regular" size:15];
//3D effects Start
BTN_For_More.layer.shadowColor= [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5f].CGColor;
BTN_For_More.layer.shadowOffset= CGSizeMake(0 ,0);//2,2
BTN_For_More.layer.shadowRadius= 2.0;
BTN_For_More.layer.shadowOpacity= 1.0;
BTN_For_More.layer.masksToBounds= NO;
//3D effects End
[Scroll_View addSubview:BTN_For_More];
y= BTN_For_More.frame.size.height + y + 20;
}//for loop END //10
Scroll_View.contentSize= CGSizeMake(Scroll_View.frame.size.width ,y);
第4步:
-(void) BTN_For_More_Action:(NSString *)myString{
NSLog(@"you clicked on button %@", myString);
}
相關問題
- 1. 如何在列表視圖上添加滾動按鈕?
- 2. 在滾動視圖上高效添加多個子視圖
- 3. 如何在滾動視圖中放置多個按鈕?
- 4. 如何滾動在水平滾動視圖一個按鈕
- 5. 如何在滾動視圖中添加文本視圖和單選按鈕?
- 6. 如何在水平滾動視圖中添加一個arow按鈕
- 7. 如何在UITableView的附件視圖中添加多個按鈕?
- 8. 如何避免在iPhone中向多個滾動視圖添加多個按鈕時出現延遲?
- 9. 如何在另一個視圖下添加動態按鈕?
- 10. 在按鈕上添加多個uitextfields按
- 11. 上的按鈕,其是在滾動視圖的頂部添加圖像
- 12. 如何在按鈕上添加多個圖像?
- 13. 按鈕滾動視圖。 jquery
- 14. Titanium一次添加多個視圖到一個滾動視圖?
- 15. 如何在按鈕上加載視圖?
- 16. 如何使滾動視圖滾動即使按鈕子視圖被按下
- 17. 如何在按鈕上滾動(水平)集合視圖?
- 18. 如何在UIToolbar上添加滾動按鈕?
- 19. 如何在表面視圖上添加圖像按鈕?
- 20. 如何在圖片上添加按鈕?
- 21. 在視圖中添加一個按鈕
- 22. 如何在集合視圖控制器上添加另一個滾動視圖?
- 23. 如何在ios 7 xcode的滾動視圖上點擊按鈕時滾動scrollview?
- 24. 如何在狀態欄上添加按鈕或任何視圖?
- 25. 如何在多個DIV按鈕中添加更多Javascript按鈕
- 26. 在視圖上方動態添加滾動視圖iphone
- 27. 我要添加按鈕的半背後滾動視圖
- 28. 將按鈕添加到帶有縮放的滾動視圖
- 29. 是否可以將按鈕添加到滾動視圖?
- 30. 嘗試添加textview按鈕點擊滾動視圖
嗨它工作完美btw按鈕寬度n buttonheight像素是正確的? – raptor85
其實你不需要擔心這一點。 iOS會將像素標準化爲X,Y座標。所以基本上你可以在iPad或iPhone上運行相同的代碼而不會遇到麻煩。 –
ty只是一個更多的問題,因爲我創建了一些按鈕,我需要添加圖像,這是在xml網址的頂部,在代碼 – raptor85