Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在UIView中不同Y位置添加多个UIButton_Objective C_Uiview_Xamarin.ios_Uibutton - Fatal编程技术网

Objective c 在UIView中不同Y位置添加多个UIButton

Objective c 在UIView中不同Y位置添加多个UIButton,objective-c,uiview,xamarin.ios,uibutton,Objective C,Uiview,Xamarin.ios,Uibutton,我知道如何以编程方式将UIButton添加到UIView。但是,在上一个位置添加了一定数量的UIButton之后,我一直在新位置添加UIButton 我的情况如下: 要添加的UIButton的数量来自NSArray-让我们选择20个 UIView已添加到UIViewController中-让我们调用 “按钮视图” 我正在使用循环创建UIButton(最多20个)。我已经设置了 按钮宽度为70,按钮宽度为45 我得到屏幕大小并用按钮除以,所以 以计算每行可容纳的项目数量 或每个Y位置 如果屏幕宽

我知道如何以编程方式将UIButton添加到UIView。但是,在上一个位置添加了一定数量的UIButton之后,我一直在新位置添加UIButton

我的情况如下:

  • 要添加的UIButton的数量来自NSArray-让我们选择20个

  • UIView已添加到UIViewController中-让我们调用
    “按钮视图”

  • 我正在使用循环创建UIButton(最多20个)。我已经设置了
    按钮宽度为70,按钮宽度为45

  • 我得到屏幕大小并用按钮除以,所以
    以计算每行可容纳的项目数量 或每个Y位置

  • 如果屏幕宽度是414,那么414/70=~5.9,我认为它是

  • 前5个UIButton的YPosition为0,XPosition从0开始,并为后续按钮添加buttonWidth

  • 接下来的5个UIButton(6-10)的新位置应为1-5 UIButton Yposition(0)+buttonHeight(45)+somespace(10)=55,Xposition应该 从0开始

  • 对于下一组UI按钮(11-15),Y位置应为
    Yposition(55)+buttonHeight(45)+somespace(10)=100和XPosition 以0开头

如何做到这一点?下面是我的代码

xPos = 8.0f; 
yPos = 0.0f;
totalSlots =timeslots.Count - //Total Number UIButtons to be creted
buttonWidth = 70;
buttonHeight = 45;
buttonViewWidth = this.view.frame.size.width // I'm getting the screen
buttonPerLine = scrollViewWidth / (buttonWidth + 10); // This calculates no.of.buttons per line/Y Position. I'm rounding off as said before
numberOfLine = totalSlots / buttonPerLine // This gives how many lines needed

for (int i = 0; i < totalSlots; i++)
                        {
timeSlotbtn[i] = new UIButton();
                        timeSlotbtn[i] = new UIButton();
                        timeSlotbtn[i].Frame = new CGRect(xPos, yPos, buttonWidth, buttonHeight);
                        timeSlotbtn[i].SetTitle(buttonText, UIControlState.Normal);
                        timeSlotbtn[i].Layer.BorderWidth = 2.0f;
                        timeSlotbtn[i].Layer.CornerRadius = 3;
                        timeSlotbtn[i].Tag = i;
                        timeSlotbtn[i].TitleLabel.Font = UIFont.FromName("HelveticaNeue-bold", 14.0f);


                xPos = timeSlotbtn[i].Frame.Location.X + buttonWidth + buttonSpace;
                timeslotScrollView.AddSubview(timeSlotbtn[i]);
}
xPos=8.0f;
yPos=0.0f;
totalSlots=timeslots.Count-//要认证的UIButtons总数
按钮宽度=70;
钮扣高度=45;
buttonViewWidth=this.view.frame.size.width//我正在查看屏幕
buttonPerLine=滚动视图宽度/(按钮宽度+10);//这将计算每行/Y位置的按钮数。如前所述,我正在逐步结束
numberOfLine=totalSlots/buttonPerLine//这给出了需要多少行
对于(int i=0;i
请注意,上面的代码是用Xamarin.iOS编写的。如果您知道解决方案,那么您可能可以用Objective-c编写。无论如何,逻辑应该是相同的。

int buttonWidth=65;
            int buttonWidth = 65;
            int buttonSpace = 5;
            int buttonHeight = 30;
            int totalSlots = timeslots.Count;
            nfloat screenSize = firstViewController.screenSize;
            nfloat buttonPerLine = screenSize / (buttonWidth + buttonSpace );
            int roundedButtonPerLine = Convert.ToInt32(Math.Floor(buttonPerLine));
            nfloat totalLines = totalSlots / buttonPerLine;
            int roundedTotalLines = Convert.ToInt32(Math.Ceiling(totalLines));
            nfloat determineXPosition = (screenSize - (8 + (buttonWidth * roundedButtonPerLine) + (buttonSpace*(roundedButtonPerLine-1))))/2;
            nfloat xPos;
            if (roundedTotalLines > 1)
            {
                 xPos = determineXPosition;
            }
            else
            {
                 xPos = 8.0f;
            }

            nfloat yPos = 0.0f;
            int currentLine = 1;
            int loopCount = 1;

            for (int i = 0; i < room.timeslots.Count; i++)
            {
                var buttonText = room.timeslots[i].startDate.ConvertDateToStanfordLocalTime();//hour + ":" + minute + "p";
                timeSlotbtn[i] = new UIButton();
                timeSlotbtn[i].Frame = new CGRect(xPos, yPos, buttonWidth, buttonHeight);
                timeSlotbtn[i].SetTitle(buttonText, UIControlState.Normal);
                timeSlotbtn[i].Layer.BorderWidth = 2.0f;
                timeSlotbtn[i].Layer.CornerRadius = 3;
                timeSlotbtn[i].Tag = i;
                timeSlotbtn[i].TitleLabel.Font = UIFont.FromName("HelveticaNeue-bold", 14.0f);

                xPos = timeSlotbtn[i].Frame.Location.X + buttonWidth + buttonSpace;
                timeslotScrollView.AddSubview(timeSlotbtn[i]);


                if (loopCount == roundedButtonPerLine)
                {

                    if (roundedTotalLines > 1)
                    {
                         xPos = determineXPosition;
                    }
                    else
                    {
                         xPos = 8.0f;
                    }
                    currentLine++;
                    yPos = timeSlotbtn[i].Frame.Location.Y + buttonHeight + buttonSpace;
                    loopCount = 0;
                }
                loopCount++;
            }
int按钮空间=5; int按钮高度=30; int totalSlots=时隙。计数; nfloat screenSize=firstViewController.screenSize; nfloat buttonPerLine=屏幕大小/(按钮宽度+按钮空间); int roundedButtonPerLine=转换为32(数学楼层(buttonPerLine)); nfloat totalLines=总插槽/按钮中线; int roundedTotalLines=转换为32(数学上限(总计线)); nfloat=屏幕大小-(8+(按钮宽度*圆形按钮基线)+(按钮空间*(圆形按钮基线-1))/2; nfloat XPO; 如果(四舍五入总直线>1) { xPos=确定的解释; } 其他的 { xPos=8.0f; } nfloat yPos=0.0f; int currentLine=1; int loopCount=1; 对于(int i=0;i1) { xPos=确定的解释; } 其他的 { xPos=8.0f; } currentLine++; yPos=timeSlotbtn[i].Frame.Location.Y+buttonHeight+buttonSpace; 循环计数=0; } loopCount++; }
我解决了这个问题。我已经更新了代码,以防有人正在寻找相同的代码。