Center UIPickerView Text

后端 未结 9 1527
清歌不尽
清歌不尽 2020-12-25 10:27

So I have a uipickerview with rows that only contain the number 0-24 and it looks a bit silly since the numbers are left aligned leaving a huge gap on the right of the picke

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-25 11:15

    I know this question is not tagged for swift, but just in case somebody is looking for the Swift 3.0 version, here it is.

    /* Called by the picker view when it needs the view to use for a given row in    
     a given component. If the previously used view (the view parameter) is adequate,    
     return that. If you return a different view, the previously used view is     
    released. The picker view centers the returned view in the rectangle for row.    
    
     */
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    
        var label = UILabel(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(300), height: CGFloat(37)))
    
    
        let booking = self.bookingInfo[row]  //bookingInfo is an array of elements
          label.text = String(booking.BookingNumber) + String(booking.DateAndTime)
            label.textAlignment = .left
             return label
    }
    

提交回复
热议问题