我有一个 UIViewController,其中我将 CALayer 子类添加到视图层:
[self.view.layer addSublayer:myObject.backgroundLayer];
Run Code Online (Sandbox Code Playgroud)
当我旋转设备时,视图会旋转,但 CALayer 不会旋转。它有点被分流到左边,仍然是纵向视图。
有没有办法让子图层自动旋转或者我需要应用变换?
我已经UIView
在nib文件中添加了一个.我想UIView
用drawRect
方法填充颜色.我怎样才能做到这一点?
我正在为视图添加子视图,我希望它填充视图的高度和宽度.我对约束有困难.任何帮助表示赞赏.这就是我目前所拥有的:
self.view.addSubview(self.mainView)
var leftSideConstraint = NSLayoutConstraint(item: self.mainView, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1.0, constant: 0.0)
var bottomConstraint = NSLayoutConstraint(item: self.mainView, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
var widthConstraint = NSLayoutConstraint(item: self.mainView, attribute: .Width, relatedBy: .Equal, toItem: self.view, attribute: .Width, multiplier: 1.0, constant: 0.0)
var heightConstraint = NSLayoutConstraint(item: self.mainView, attribute: .Height, relatedBy: .Equal, toItem: self.view, attribute: .Height, multiplier: 1.0, constant: 0.0)
self.view.addConstraints([leftSideConstraint, bottomConstraint, widthConstraint, heightConstraint])
Run Code Online (Sandbox Code Playgroud) 我有一个带有排序按钮的表视图控制器,我用它来允许用户更改tableview的排序顺序.按下按钮时,我会转到排序视图控制器,并传入当前排序顺序,UIPickerView将其选中并设置为所选项目.
如果用户更改了选择器中的选定项,我想将其发送回父视图控制器,以便它可以重新排序表.
到目前为止,我已经看到了两种方法:设置委托或使用NSNotificationCenter.我决定试试后者.
在我的SortViewController中,我捕获任何更改pickerView:didSelectRow:InComponent,然后将新值作为命名通知发布.
在父视图控制器中,我在视图控制器的init中添加了一个addObserver调用,以便它正在侦听该通知,但显然从未发送通知.
这是代码:
TableViewController:
var sortOrder: String = "Name"
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "setSortOrder:",
name: "sortOrderChangedNotification",
object: sortOrder )
Run Code Online (Sandbox Code Playgroud)
接收功能:
func setSortOrder( notification: NSNotification ) {
// set the sort order
println("Received sortOrderChangedNotification")
}
Run Code Online (Sandbox Code Playgroud)
SortViewController:
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) {
println("Picker view selection changed!")
var selRow = sortPickerView.selectedRowInComponent(0)
NSNotificationCenter.defaultCenter().postNotificationName("sortOrderChangedNotification", object: sortMethods[selRow] )
}
Run Code Online (Sandbox Code Playgroud)
我从来没有得到setSortOrder的println输出,据我所知,这可能意味着a)通知没有被推送(即使didSelectRow中的println打印)b)我的观察者没有收到它或c )我的观察者设置错了.
任何输入将不胜感激.
如何以编程方式修改约束的乘数?我设置了以下内容:
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0.0]];
Run Code Online (Sandbox Code Playgroud)
我需要将其修改为:
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0]];
Run Code Online (Sandbox Code Playgroud) 我想将正值转换为负值,例如:
let a: Int = 10
Run Code Online (Sandbox Code Playgroud)
转到它-10
,我当前的想法是将其用于多个-1
a * -1
Run Code Online (Sandbox Code Playgroud)
我不确定这是否正确,知道吗?
ios ×4
swift ×3
autolayout ×2
uiview ×2
calayer ×1
cocoa-touch ×1
constraints ×1
drawrect ×1
fill ×1
iphone ×1
negate ×1
objective-c ×1