我创建了一个自定义的UITableViewCell,它在我的视图控制器中以编程方式添加。自定义单元格包含一个按钮。问题是我无法实现按钮在所有设备上居中。如果它是5s模拟器,我使用self.frame.width作为按钮框架的x坐标,那么它是居中的 - 但不是6或6s,但是如果我使用super.frame.width x-coord,然后以6模拟器为中心。有谁知道如何让它集中在所有设备屏幕上?这是我的自定义按钮的代码,我只是将它作为UITableViewCell返回到我的viewcontroller中的tableView委托......
import Foundation
import UIKit
public class ButtonCell: UITableViewCell {
//Custom button to be added to the last cell to submit
let buttonImage = UIImage(named: "add_pub_button") as UIImage?
let button = UIButton(type: UIButtonType.Custom) as UIButton
/**
Creates the ButtonCell
- parameter style: A constant indicating a cell style. See UITableViewCellStyle for descriptions of these constants.
- parameter reuseIdentifier: A string used to identify the cell object if it is to be reused for drawing multiple rows of a table view. Pass nil if the cell object is not to be reused. You should use the same reuse identifier for all cells of the same form.
- returns: An initialized TextInputCell object or nil if the object could not be created.
*/
override public init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setup()
}
private func setup() {
//Overhangs the view slightly to avoid invalid constraints.
self.clipsToBounds = true
self.backgroundView = nil
self.backgroundColor = nil
self.tintColor = nil
self.selectionStyle = .None
button.frame = CGRectMake((self.frame.width - 134)/2, 0, 134, 44)
button.tintColor = nil
button.backgroundColor = nil
button.setImage(buttonImage, forState: .Normal)
self.contentView.addSubview(button)
}
/**
Needed for initialization from a storyboard.
- parameter aDecoder: An unarchiver object.
- returns: An initialized ButtonCell object or nil if the object could not be created.
*/
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
}
答案 0 :(得分:0)
我不会使用这个框架。我会用constraints。您可以使用以下方法轻松创建一个水平居中的约束:
button.translatesAutoresizingMaskIntoConstraints = false
let centerX = NSLayoutConstraint(item: button, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0)
self.addConstraint(centerX)
答案 1 :(得分:0)
只需在SyntaxError: /Users/KOSUKE/workspace/score_share/app/models/user.rb:6: syntax error, unexpected ':', expecting keyword_end
format: { with: VALID_EMAIL_REGEX }
^
/Users/KOSUKE/workspace/score_share/app/models/user.rb:7: syntax error, unexpected ':', expecting keyword_end
uniqueness: { case_sensitive: false }
^
from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:457:in `load'
from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:457:in `block in load_file'
from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:647:in `new_constants_in'
from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:456:in `load_file'
from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:354:in `require_or_load'
from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:494:in `load_missing_constant'
from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:184:in `const_missing'
from (irb):1
from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/console.rb:110:in `start'
from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/console.rb:9:in `start'
from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require'
from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `block in require'
from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require'
from /Users/KOSUKE/workspace/score_share/bin/rails:9:in `<top (required)>'
from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load'
from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load'
from /Users/KOSUKE/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/KOSUKE/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'2.2.1 :002 >
方法
例如,
layoutSubviews