LoginSignup
8
8

More than 5 years have passed since last update.

interfacebuilderとAuto layoutで1pxの線を出す

Last updated at Posted at 2014-11-13

CALayerなんかを使ってUIVewに線を追加するやり方なんかはわりとよくあるのだけど、InterfaceBuilder上でいい感じの位置に線を出したいとなるとちょいと工夫が必要だったのでメモ

まずは結果から


求められているのは0.5の線。
いまいち理解が甘いのですがAutolayoutでHeight=1を設定しても2px程度になるようです。

新規に設定する場合

まずはさくっとviewを配置

そしてAutolayoutの設定で Heightに.5と入力

ここまではOK。

途中から設定する場合

しかし上記のHeight 0.5、初回にかけてあげることはできるようですが、

こちらのSize Inspectorからは設定できない模様・・・・

ちょっと新規でやりなおすのもな・・・という時は、下記のやり方もあるようです。

こんな感じでconstraintとoutletを紐付けて

class ViewController: UIViewController {
    // constraint outlet
    @IBOutlet weak var heightConstraint: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        heightConstraint.constant = 0.5
    }

}

こんな具合でconstraintを変更してやることでも可能なようです。

8
8
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
8
8