【问题标题】:Text alignment issue by programatically created buttons and View以编程方式创建的按钮和视图导致的文本对齐问题
【发布时间】:2020-10-26 05:12:02
【问题描述】:

我有一个以编程方式创建的视图,里面有 8 个按钮(一行 3 个按钮)。我有动态按钮标题,所以我的对齐方式搞砸了。 我想要的是左中心和右对齐的按钮,如果更多的文本应该有 2 行。

CreateBtn 函数创建按钮 视图宽度为 375,高度为 130

image

func CreateBtn() {
        
        let workingImage = UIImage(named: "WorkStatus") as UIImage?
        let workingButton = UIButton(frame: CGRect(x: -53, y: 0, width: 200, height: 60))
        workingButton.setTitle("\(localWorking)", for: .normal)
        workingButton.setImage(workingImage, for: .normal)
        workingButton.setTitleColor(UIColor.rgb(red: 93, green: 93, blue: 93), for: .normal)
        workingButton.addTarget(self, action: #selector(self.WorkingbuttonTapped), for: .touchUpInside)
        view.addSubview(workingButton)
        
        let idleImage = UIImage(named: "Idle") as UIImage?
        let idleButton = UIButton(frame: CGRect(x: 58, y: 0, width: 200, height: 60))
        idleButton.setTitle("\(localIdle)", for: .normal)
        idleButton.setImage(idleImage, for: .normal)
        
        idleButton.setTitleColor(UIColor.rgb(red: 93, green: 93, blue: 93), for: .normal)
        idleButton.addTarget(self, action: #selector(self.IdlebuttonTapped), for: .touchUpInside)
        view.addSubview(idleButton)
        
        let meetingImage = UIImage(named: "Meeting") as UIImage?
        let mettingButton = UIButton(frame: CGRect(x: 190, y: 0, width: 200, height: 60))
        mettingButton.setTitle("\(localMeeting)", for: .normal)
        mettingButton.setImage(meetingImage, for: .normal)
        mettingButton.setTitleColor(UIColor.rgb(red: 93, green: 93, blue: 93), for: .normal)
        mettingButton.addTarget(self, action: #selector(self.MeetingbuttonTapped), for: .touchUpInside)
        view.addSubview(mettingButton)
        
        let lunchImage = UIImage(named: "Lunch") as UIImage?
        let lunchButton = UIButton(frame: CGRect(x: -60, y: 40, width: 200, height: 60))
        lunchButton.setTitle("\(localLunch)", for: .normal)
        lunchButton.setImage(lunchImage, for: .normal)
        lunchButton.setTitleColor(UIColor.rgb(red: 93, green: 93, blue: 93), for: .normal)
        lunchButton.addTarget(self, action: #selector(self.LunchbuttonTapped), for: .touchUpInside)
        view.addSubview(lunchButton)
        
        let checkinImage = UIImage(named: "Check In") as UIImage?
        let checkinButton = UIButton(frame: CGRect(x: 75, y: 40, width: 200, height: 60))
        checkinButton.setTitle("\(localCheckin)", for: .normal)
        checkinButton.setImage(checkinImage, for: .normal)
        checkinButton.setTitleColor(UIColor.rgb(red: 93, green: 93, blue: 93), for: .normal)
        checkinButton.addTarget(self, action: #selector(self.CheckInbuttonTapped), for: .touchUpInside)
        view.addSubview(checkinButton)
        
        let checkoutImage = UIImage(named: "Check Out") as UIImage?
        let checkoutButton = UIButton(frame: CGRect(x: 198, y: 40, width: 200, height: 60))
        checkoutButton.setTitle("\(localCheckOut)", for: .normal)
        checkoutButton.setImage(checkoutImage, for: .normal)
        checkoutButton.setTitleColor(UIColor.rgb(red: 93, green: 93, blue: 93), for: .normal)
        checkoutButton.addTarget(self, action: #selector(self.CheckOutbuttonTapped), for: .touchUpInside)
        view.addSubview(checkoutButton)
        
        let onMobileImage = UIImage(named: "On Mobile") as UIImage?
        let onMobileButton = UIButton(frame: CGRect(x: -45, y: 80, width: 200, height: 60))
        onMobileButton.setTitle("\(localOnMobile)", for: .normal)
        onMobileButton.setImage(onMobileImage, for: .normal)
        onMobileButton.setTitleColor(UIColor.rgb(red: 93, green: 93, blue: 93), for: .normal)
        onMobileButton.addTarget(self, action: #selector(self.OnMobilebuttonTapped), for: .touchUpInside)
        view.addSubview(onMobileButton)
        
        let privateImage = UIImage(named: "Private Time") as UIImage?
        let privateButton = UIButton(frame: CGRect(x: 90, y: 80, width: 200, height: 60))
        privateButton.setTitle("\(localPrivateTime)", for: .normal)
        privateButton.setImage(privateImage, for: .normal)
        privateButton.setTitleColor(UIColor.rgb(red: 93, green: 93, blue: 93), for: .normal)
        privateButton.addTarget(self, action: #selector(self.PrivateTimebuttonTapped), for: .touchUpInside)
        view.addSubview(privateButton)
    }

【问题讨论】:

  • 我不明白你所说的左对齐和右对齐是什么意思但是为了让你的文本分成两行你必须修复你的按钮宽度(通过设置 wdth 锚)然后将行数设置为两行。这可能会有所帮助stackoverflow.com/questions/30679370/…
  • 请使用约束...

标签: ios swift uibutton text-alignment


【解决方案1】:

您是否尝试过使用UIStackView? 以下是对 Apple 文档的参考:https://developer.apple.com/documentation/uikit/uistackview

如果您使用堆栈视图,您将能够将按钮对齐到顶部/底部/左侧等。

因此,您将前 3 个项目(比如说“列”)放入垂直堆栈视图中。然后你在另外两列上重复它。然后将所有 3 列放入水平堆栈视图中。这将有助于轻松调整您的需求。在将项目放入堆栈视图之前,不要忘记设置宽度和高度限制。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多