簡體   English   中英

UIView內的UIScrollView

[英]UIScrollView inside UIView

我在一個項目中,我在UIView中有一個UIScrollView。 在UIScrollView內部必須查看3列和X行的按鈕列表。 我有顯示按鈕的代碼,但是沒有插入到UIScrollView中,它會打印並且不會滾動。 除此之外,還要覆蓋視圖底部的TabBar。

顯示按鈕的功能代碼為

for (int y_axis=0; y_axis<=3; y_axis++)
    {
        for (int x_axis=0; x_axis<=2; x_axis++)
        {

            UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(16+100*x_axis,100.0+115*y_axis,88.0 ,88.0)];
            btn.backgroundColor=[UIColor brownColor];
            [self.view addSubview:btn];

        }
    }

如何在UIScrollView中顯示此內容? 謝謝!!

假設您有一個UIScrollView滾動器,則必須將最后一行更改為此。

[self.scroller addSubview:btn];

如果不是,則將其添加到視圖中,而不是滾動器中。 此外,還必須更改滾動條的contentsize,如果包含多少則為X * Y。 它包含整個屏幕,僅此而已;

[self.scroller setContentSize:CGSizeMake(320, 480)]
                                         ^     ^

將其替換為要滾動的寬度和高度。 每次在新按鈕下方添加一個按鈕時,都應更新contentsize。

這是因為您要在視圖[self.view addSubview:btn];上添加按鈕[self.view addSubview:btn]; 不在ScrollView 嘗試使用所有按鈕進行查看。 並設置有按鍵為您的視圖contentview您的scrollview

首先,您需要在.h文件中使用UIScrollViewDelegate ,然后還檢查按鈕是否為ScrollView的子視圖,也可以使用以下代碼在滾動視圖中添加此按鈕...

[yourScrollView addSubview:yourbuttonname];

之后,在您的viewDidLoad方法中像這樣聲明您的scrollview的contentsize ...

yourScrollView.contentSize=CGSizeMake(320, anyheight);///here define height which you want to scroll the view...

希望對您有幫助.... :)

請嘗試以下步驟:

    for loop
    {
        create buttons along with the desired frames;
        add those buttons to scrollview;
    }
    set the content size of the scrollview according to no of rows;
    add the scroll view to main view i.e. self.view;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM