2017-06-14 21 views
0

我有一個按鈕,我可以用這個代碼很容易找到:Swift3 - 無法找到UISegmentedControl編程

let btnRewind = UIButton(
    frame:CGRect(
     x:(UIScreen.main.bounds.size.height/2 - 4*UIScreen.main.bounds.size.height/30), 
     y:(UIScreen.main.bounds.size.width/2 - UIScreen.main.bounds.size.height/30), 
     width:(UIScreen.main.bounds.size.height/15), 
     height:(UIScreen.main.bounds.size.height/15) 
    ) 
) 

但相同的代碼不適合我的工作UISegmentedControl。這就是x:0 y:0。我在想什麼:/

var sgmSpeed = UISegmentedControl(
    frame:CGRect(
     x:(UIScreen.main.bounds.size.height/2 - UIScreen.main.bounds.size.height/30), 
     y:(UIScreen.main.bounds.size.width/2 - UIScreen.main.bounds.size.height/30), 
     width:(UIScreen.main.bounds.size.height/15), 
     height:(UIScreen.main.bounds.size.width/15) 

    ) 
) 
+1

嘗試使用'initWithItems'構造。文檔稱「自動調整大小以適應其內容」。 –

回答

0

這一個是工作,感謝菲利普·米爾斯

sgmSpeed.frame = CGRect.init(x:(UIScreen.main.bounds.size.height/2 - UIScreen.main.bounds.size.height/6), 
           y:(UIScreen.main.bounds.size.width/2 + UIScreen.main.bounds.size.height/15), 
           width:(UIScreen.main.bounds.size.height/3), 
           height:(UIScreen.main.bounds.size.width/15))