Nick
Nick

Reputation: 1444

Set UIButton tittle to specific position for all screen sizes

Below I have a button.

enter image description here

It is actually a UIButton with an Image inside. The problem is that the "Scan Product" is inside the image too, isn't a title.

First of all I want to know if apple allows this, cause I can't find something in the guidelines.

Also is there a way to place the title lets say at 25% of the buttons height.

EDIT

scanTest.setTitle("Test", for: .normal)
        let x = scanTest.frame.origin.x * 0.35
        scanTest.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: x, right: 0)

This is to place the title at 25% of the buttons height. But on iPhone SE looks wrong.

enter image description here

I mean I don't think that this is the 25%.

Upvotes: 0

Views: 146

Answers (3)

blue
blue

Reputation: 90

I recommend using AutoLayout for everything, so you can add an invisible UIView on top of your image that has 25% of the image's height and then just add the button to that view and make sure that the topAnchor of the button is the same as the new UIView.

Let me know if you couldn't understand so I can make you an example.

UPDATE:

You can try to run this, it may not be the optimal solution but it will do the job. (you can play with background colors to understand the architecture)

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    view.backgroundColor = .white

    let imageView = UIImageView()
    view.addSubview(imageView)
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

    let holder = UIView()
    view.addSubview(holder)
    holder.translatesAutoresizingMaskIntoConstraints = false
    holder.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.25).isActive = true
    holder.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    holder.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    holder.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

    let label = UILabel()
    holder.addSubview(label)
    label.translatesAutoresizingMaskIntoConstraints = false
    label.topAnchor.constraint(equalTo: holder.topAnchor).isActive = true
    label.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    label.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    label.text = "Hello"
    label.textAlignment = .center

}

Upvotes: 1

amit
amit

Reputation: 425

you can set the title and image according to title and image insets via 'size inspector'. One more way you can take one view and inside that take UIImage(your image) and UILabel(put your title) and above both UIButton for more customisation.

Upvotes: 0

Shivang Pandey
Shivang Pandey

Reputation: 188

Answer of first question :- Apple will definitely allow this.

Answer of Second question : - Because title is inside image ,title cannot change from xcode but you can edit image.

Upvotes: 1

Related Questions