【发布时间】:2016-03-06 08:22:32
【问题描述】:
我以编程方式创建了 UIView 和 Button
var width = self.view.frame.width - 8
var height = width/8
newView.frame = CGRectMake(4, 39, width, height)
newView.backgroundColor = UIColor.greenColor()
self.view.addSubview(newView)
var button = UIButton.buttonWithType(UIButtonType.System) as! UIButton
button.frame = newView.frame
button.backgroundColor = UIColor.whiteColor()
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
button.setTitle(" All Hospitals/Clinics", forState: UIControlState.Normal)
button.setTitleColor(UIColor.blackColor(), forState: .Normal)
button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left
newView.addSubview(button)
我希望按钮位于 UIView 内部,并且与 UIView 具有完全相同的位置、宽度和高度
但结果是这样的
我的代码有什么问题?
提前致谢
【问题讨论】:
-
你在使用自动布局吗?
-
@SohilR.Memon 是的,我正在使用自动布局,但我需要以编程方式创建此按钮
-
那么,您还必须在以编程方式创建 UI 时给予约束!
-
@SohilR.Memon UIView 和按钮的约束?这是这个问题的原因吗?即使他们有相同的框架?