LoginSignup
0
0

More than 1 year has passed since last update.

Buttonを押すとSegmentが減る(UISegmentedControl)

Posted at

今回の内容

Buttonを押すとSegmentが増える(UISegmentedControl)の続きになります。

機能説明

  • Buttonを押すと、UISegmentedControlのSegmentが減る。

コードと簡単解説

  • Segmentを削除するときは、右側から削除していきたいので現在のsegmentCountとsegmentContentsArrayの最大値が同じなら、.removeSegment(at: segmentCount, animated: true) で一番右のSegmentを削除します。

  • .segmentContentsArray.remove(at: segmentCount)の後にsegmentCount -= 1をすることで、segmentContentsArrayの最大値とsegmentCountを同じにしておかないと、次にSegmentを増やす時に見た目がおかしくなります。

  • Segmentを削除するときも、増やす時と同様にframeのwidthを引いています。

ViewController
 @IBAction func minusSegment(_ sender: Any) {

        if segmentControl.segmentContentsArray[segmentCount] == String(segmentCount){

            segmentControl.uiSegmentControl.removeSegment(at: segmentCount, animated: true)

            segmentControl.segmentContentsArray.remove(at: segmentCount)

            segmentCount -= 1

            segmentControl.uiSegmentControl.frame = CGRect(x: segmentControl.uiSegmentControl.bounds.minX, y: segmentControl.uiSegmentControl.bounds.minY + 100, width: segmentControl.uiSegmentControl.frame.width - 35, height: segmentControl.uiSegmentControl.frame.size.height)
        }

    }

終わり

昨日のUISegmentedControlを増やすだけのアプリに減らす機能を作りました。
ご指摘、ご質問などありましたら、コメントまでお願い致します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0